使用UNION细化的SQL查询

时间:2015-11-21 14:31:48

标签: sql oracle11g union

我想知道是否有更好的方法来编码以下要求: 我正在使用Oracle 11.x db

表格设置:

vector

表1 -

create table S_ASSET (ROW_ID varchar2(10), X_QUOTE_ID varchar2(10), SP_NUM varchar2(10), AMT number(10), ASSET_NUM varchar2(10));
create table S_QUOTE_ITEM (ROW_ID varchar2(10), AMT number(10));
create table S_QUOTE_ITEM_XM (PAR_ROW_ID varchar2(10), AMT number(10));

表2 -

insert into S_ASSET (ROW_ID, X_QUOTE_ID, SP_NUM, AMT, ASSET_NUM) values ('1', 'A1', '000', 10, 'AAA');
insert into S_ASSET (ROW_ID, X_QUOTE_ID, SP_NUM, AMT, ASSET_NUM) values ('1', 'A2', '000', 20, 'AAA');
insert into S_ASSET (ROW_ID, X_QUOTE_ID, SP_NUM, AMT, ASSET_NUM) values ('2', 'B1', '111', '', 'BBB');
insert into S_ASSET (ROW_ID, X_QUOTE_ID, SP_NUM, AMT, ASSET_NUM) values ('3', 'C1', '222', 10, 'CCC');
insert into S_ASSET (ROW_ID, X_QUOTE_ID, SP_NUM, AMT, ASSET_NUM) values ('3', 'C2', '222', 0, 'CCC');
insert into S_ASSET (ROW_ID, X_QUOTE_ID, SP_NUM, AMT, ASSET_NUM) values ('4', 'D1', '333', 10, 'DDD');
insert into S_ASSET (ROW_ID, X_QUOTE_ID, SP_NUM, AMT, ASSET_NUM) values ('5', 'E1', '444', 0, 'EEE');

表3 -

insert into S_QUOTE_ITEM (ROW_ID, AMT) values ('A1', 5);
insert into S_QUOTE_ITEM (ROW_ID, AMT) values ('A2', '');
insert into S_QUOTE_ITEM (ROW_ID, AMT) values ('B1', 0);
insert into S_QUOTE_ITEM (ROW_ID, AMT) values ('C1', 5);
insert into S_QUOTE_ITEM (ROW_ID, AMT) values ('C2', 0);
insert into S_QUOTE_ITEM (ROW_ID, AMT) values ('D1', '');
insert into S_QUOTE_ITEM (ROW_ID, AMT) values ('E1', 5);

其中x-quote_id(表1)= row_id(表2)= par_row_id(表3)

我需要按sp_num分组的AMT列的总和。以下是我写的查询。我正在寻找一种更简单,更有效的方法来编写相同的内容。

insert into S_QUOTE_ITEM_XM (PAR_ROW_ID, AMT) values ('A1', 1);
insert into S_QUOTE_ITEM_XM (PAR_ROW_ID, AMT) values ('A1', '');
insert into S_QUOTE_ITEM_XM (PAR_ROW_ID, AMT) values ('B1', 1);

预期的O / P:

select sp_num, sum(amt) as DocFee
from
(   
    (
        select x_quote_id row_id, amt, sp_num
        from s_asset
    )
    union
    (   
        select a.row_id, a.amt, b.sp_num
        from
        (   
            select row_id, sum(amt) amt
            from s_quote_item
            group by row_id
        ) a,
        s_asset b
        where b.x_quote_id = a.row_id
    )
    union      
    (
        select a.par_row_id row_id, a.amt, b.sp_num
        from
        (
            select par_row_id, sum(amt) amt
            from s_quote_item_xm
            group by par_row_id
        ) a,
        s_asset b
        where b.x_quote_id = a.par_row_id
    )
)
group by sp_num 
order by sp_num

感谢所有提出有效解决方案的人。然而,我正在努力将下面的查询结合起来(因为我是完全新的SQL),所有这些都有一个共同的因素sp_num

查询1 :(父)

sp_num    DocFee
000       36
111       1
222       15
333       10
444       5

查询2:(GP)

select  POL.SP_NUM POL#
,       POL.ASSET_NUM COV#
,       sum(POL.AMT) SI
from S_ASSET POL
group by
        POL.ASSET_NUM
,        POL.SP_NUM
order by POL.sp_num

查询3:(DOCFEE)(以下或胡安卡洛斯在此主题中共享的结果)

select 
  sp_num as pol#,
  coalesce(sum(
    (
      select sum(deb.amt)
      from s_invoice deb
      where deb.fn_accnt_id = pol.row_id
      and deb.debit_type = 'Customer' 
    )
  ), 0) -
  coalesce(sum(
    (
      select sum(cred.amt) 
      from s_src_payment cred
       where cred.asset_id = pol.row_id
      and cred.cg_dedn_type_cd = 'Customer' 
    )), 0)
   as gp
from s_asset pol 
group by sp_num
order by sp_num

2 个答案:

答案 0 :(得分:1)

<强> SQL Fiddle Demo

WITH A as (
     SELECT sp_num, X_QUOTE_ID, sum(AMT) AMT
     FROM S_ASSET
     GROUP BY sp_num, X_QUOTE_ID
), B as (
     SELECT ROW_ID, sum(AMT) AMT
     FROM S_QUOTE_ITEM
     GROUP BY ROW_ID
), C as (
     SELECT PAR_ROW_ID, sum(AMT) AMT
     FROM S_QUOTE_ITEM_XM
     GROUP BY PAR_ROW_ID  
)
SELECT sp_num, COALESCE(SUM(A.AMT),0) + 
               COALESCE(SUM(B.AMT),0) + 
               COALESCE(SUM(C.AMT),0) DOCFEE
FROM A
LEFT JOIN B
       ON A.X_QUOTE_ID = B.ROW_ID
LEFT JOIN C
       ON A.X_QUOTE_ID = C.PAR_ROW_ID
GROUP BY sp_num     
ORDER BY sp_num;

答案 1 :(得分:0)

我认为最好在sp_num级别聚合每个子表,然后进行最终聚合:

select sp_num, sum(amt)
from ((select a.sp_num, sum(a.amt) as amt
       from s_asset a
       group by a.sp_num
      ) union all
      (select a.sp_num, sum(q.amt) as amt
       from s_asset a join
            s_quote_item q
            on q.row_id = a.x_quote_id
       group by a.sp_num
      ) union all
      (select a.sp_num, sum(x.amt) as amt
       from s_asset a join
            s_quote_item_xm x
            on x.par_row_id = a.x_quote_id
       group by a.sp_num
      )
     ) aqx
group by sp_num;

如果您愿意,可以通过删除第一级聚合来缩短查询:

select sp_num, sum(amt)
from ((select a.sp_num, a.amt as amt
       from s_asset a
      ) union all
      (select a.sp_num, q.amt as amt
       from s_asset a join
            s_quote_item q
            on q.row_id = a.x_quote_id
      ) union all
      (select a.sp_num, x.amt as amt
       from s_asset a join
            s_quote_item_xm x
            on x.par_row_id = a.x_quote_id
      )
     ) aqx
group by sp_num;

我的猜测是,在大多数情况下,Oracle会为第一个版本制定更好的执行计划。