有人可以帮我吗?我只是一个初学者

时间:2019-05-26 08:10:20

标签: sql

Select 
    m.style_id,
    sum(p.production_qty) Cutting 
From 
    Master_Data m 
Left Join 
    production_data p on P.Style_ID = M.Style_ID 
where 
    p.production_process = 'Cutting', 

    sum(p.production_qty) Induction 
From 
    Master_Data m 
Left Join 
    production_data p on P.Style_ID = M.Style_ID 
where 
    p.production_process = 'Induction', 
group by 
    m.style_id

如何获得两个不同的select语句的结果?

2 个答案:

答案 0 :(得分:2)

您可以使用条件聚合来获得所需的结果:

Select 
    m.style_id,
    sum(case when p.production_process = 'Cutting' then p.production_qty else 0 end) Cutting 
    sum(case when p.production_process = 'Induction' then p.production_qty else 0 end) Induction 
From 
    Master_Data m 
Left Join 
    production_data p on P.Style_ID = M.Style_ID 
where 
    p.production_process in ('Induction', 'Cutting')
group by 
    m.style_id

答案 1 :(得分:0)

根据您希望结果集的外观,可以使用聚合:

Select p.production_process, m.style_id,
       sum(p.production_qty) Cutting 
From Master_Data m left join 
     production_data p
     on p.Style_ID = m.Style_ID and
        p.production_process in ('Cutting', 'Induction')
group by p.production_process, m.style_id;