将多个子查询合并为一个查询

时间:2018-09-28 11:36:44

标签: sql oracle subquery

我正在尝试合并多个子查询以形成详细说明处于“进行中”状态的报告顺序。长话短说,订单要经历多个阶段,客户希望获得每个阶段单位级别的总库存。

这是我尝试将前两个结合在一起的想法,以为如果我做不到,我将无法添加另外两个。

select In_Progress_Total.purchase_order, In_Progress, In_Progress_Awaiting_Vas
from
(   select order_header.purchase_order, move_task.work_group,SUM(move_task.qty_to_move) AS In_Progress
    from move_task
    join order_header
    on move_task.task_id = order_header.order_id
    where move_task.site_id = 'USCOL1' and move_task.client_id = 'SDRY-US' and order_header.status = 'In Progress'
    group by order_header.purchase_order, move_task.work_group); as In_Progress_Total
join
(   select order_header.purchase_order, move_task.work_group, SUM(move_task.qty_to_move) AS In_Progress_Awaiting_Vas
    from move_task
    join order_header
    on move_task.task_id = order_header.order_id
    where move_task.site_id = 'USCOL1' and move_task.client_id = 'SDRY-US' and move_task.from_loc_id like 'DEPT%VAS' and order_header.status = 'In Progress'
    group by order_header.purchase_order, move_task.work_group); as Awaiting_Vas)
on In_Progress_Total.purchase_order = Awaiting_Vas.purchase_order
order by In_Progress_Total.purchase_order

发生这两种情况时,它会将查询作为两个查询而不是一个查询运行,我得到以下错误(一旦解决将导致另一个错误):

Multiple Queries / Error

另外两个查询组合在一起:

select order_header.purchase_order, move_task.work_group,SUM(move_task.qty_to_move) AS In_Progress_Awaiting_Labels
from move_task
join order_header
on move_task.task_id = order_header.order_id
where move_task.site_id = 'USCOL1' and move_task.client_id = 'SDRY-US' and move_task.from_loc_id like 'OUTVAS' and order_header.status = 'In Progress'
group by order_header.purchase_order, move_task.work_group


select order_header.purchase_order, move_task.work_group,SUM(move_task.qty_to_move) AS Everything_Else
from move_task
join order_header
on move_task.task_id = order_header.order_id
where move_task.site_id = 'USCOL1' and move_task.client_id = 'SDRY-US' and order_header.status = 'In Progress' and move_task.from_loc_id not like '%OUTVAS%' and move_task.from_loc_id not like 'DEPT%VAS' 
group by order_header.purchase_order, move_task.work_group

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

请考虑使用条件聚合,您可以将WHERE个条件移至CASE条针对不同SUM聚合的语句中。确保还使用表别名以提高可读性。

SELECT o.purchase_order, m.work_group,
       SUM(CASE 
               WHEN m.site_id = 'USCOL1' AND 
                    m.client_id = 'SDRY-US' AND 
                    o.status = 'In Progress'
               THEN m.qty_to_move
               ELSE NULL
           END) AS In_Progress,

       SUM(CASE 
               WHEN m.site_id = 'USCOL1' AND 
                    m.client_id = 'SDRY-US' AND 
                    m.from_loc_id LIKE 'DEPT%VAS' AND 
                    o.status = 'In Progress'
               THEN m.qty_to_move
               ELSE NULL
           END) AS In_Progress_Awaiting_Vas,

       SUM(CASE
               WHEN m.site_id = 'USCOL1' AND 
                    m.client_id = 'SDRY-US' AND 
                    m.from_loc_id LIKE 'OUTVAS' AND 
                    o.status = 'In Progress'
               THEN m.qty_to_move
               ELSE NULL
           END) AS In_Progress_Awaiting_Labels,

       SUM(CASE
               WHEN m.site_id = 'USCOL1' AND 
                    m.client_id = 'SDRY-US' AND 
                    o.status = 'In Progress' AND 
                    m.from_loc_id NOT LIKE '%OUTVAS%' AND 
                    m.from_loc_id NOT LIKE 'DEPT%VAS'
               THEN m.qty_to_move
               ELSE NULL
           END) AS Everything_Else

FROM move_task m
JOIN order_header o ON m.task_id = o.order_id 
GROUP BY o.purchase_order, m.work_group