将多个子查询组合成一个查询
Posted
技术标签:
【中文标题】将多个子查询组合成一个查询【英文标题】:Combining multiple sub-queries into a single query 【发布时间】:2018-09-28 11:36:44 【问题描述】:我正在尝试组合几个子查询以形成处于“进行中”状态的报告详细订单。长话短说,订单经历了多个阶段,客户希望在单位级别获得每个阶段的总库存。
这是我将前两个结合起来的尝试,我的想法是如果我做不到,我就无法添加另外两个。
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】:考虑使用条件聚合,将WHERE
条件移动到不同SUM
聚合的CASE
语句中。确保还使用表别名以提高可读性。
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
【讨论】:
以上是关于将多个子查询组合成一个查询的主要内容,如果未能解决你的问题,请参考以下文章