Oracle SQL:多个枢纽

时间:2018-10-17 15:31:55

标签: sql oracle pivot

我具有以下架构,并使用2个枢轴构建了查询 sqlfiddle

select * from 
    (select * from 
        (select order_id, product_id, adl_qty, kid_qty, status, 
        case 
            when status = 'DUE OUT' and package_name = 'BRF' then 'BRF' 
            when status = 'ARRIVAL' and package_name IN ('LNC', 'DNR') then package_name 
            when status = 'STAY OVER' and package_name IN ('BRF', 'LNC', 'DNR') then package_name 
            --else package_name 
        end  
        adl_package, 

        case 
            when status = 'DUE OUT' and package_name = 'KIDBRF' then 'KIDBRF' 
            when status = 'ARRIVAL' and package_name IN ('KIDLNC', 'KIDDNR') then package_name 
            when status = 'STAY OVER' and package_name IN ('KIDBRF', 'KIDLNC', 'KIDDNR') then package_name 
            --else package_name 
        end  
        kid_package 

        from orders 
        left join product_packages using (product_id) 
        left join packages using (package_id) 
        order by status, adl_package) 
    pivot  
    ( 
        max(adl_qty)  
        for adl_package in ('BRF' brf, 'LNC' lnc, 'DNR' dnr)  
    ) 
    ) 
    pivot 
    ( 
        max(kid_qty)  
        for kid_package in ('KIDBRF' kidbrf, 'KIDLNC' kidlnc, 'KIDDNR' kiddnr)  
    ) 
    order by product_id

问题是每个枢轴创建一行(每个order_id为2行),我实际上想将2个枢轴合并为1行。结果应如下所示:

ORDER_ID  PRODUCT_ID  STATUS    BRF  LNC  DNR  KIDBRF  KIDLNC  KIDDNR
--------  ----------  ------    ---  ---  ---  ------  ------  ------
       1           2  DUE OUT     1                                  
       2           3  ARRIVAL          2    2               1       1
       3           3  STAY OVER   1    1    1       2       2       2

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

使用条件聚合代替pivot

select order_id, product_id status,
       sum(case when adl_package = 'BRF' then adl_qty else 0 end) as brf,
       sum(case when adl_package = 'LNC' then adl_qty else 0 end) as lnc,
       sum(case when adl_package = 'DNR' then adl_qty else 0 end) as dnr,
       sum(case when kid_package = 'KIDBRF' then kid_qty else 0 end) as kidbrf,
       sum(case when kid_package = 'KIDLNC' then kid_qty else 0 end) as kidlnc,
       sum(case when kid_package = 'KIDDNR' then kid_qty else 0 end) as kiddnr
from orders o left join
     product_packages pp
     using (product_id) left join
     packages p
     using (package_id) 
group by order_id, product_id status;

这使用sum()而不是max()`。