0

SELECT lp.lead_bucket_no , case when p.product = 'S-400' then qty end as 'S400' , case when p.product = 'Dish Antenna' then qty end as 'DishAntenna' FROM lead_products lp INNER JOIN products p ON p.product_id = lp.product_id WHERE type = 'stock' GROUP BY lead_bucket_no ORDER BY lp.lead_bucket_no

SELECT lp.lead_bucket_no , case when p.product = 'S-400' then qty end as 'S400' , case when p.product = 'Dish Antenna' then qty end as 'DishAntenna' FROM lead_products lp INNER JOIN products p ON p.product_id = lp.product_id WHERE type = 'order' GROUP BY lead_bucket_no ORDER BY lp.lead_bucket_no

如何合并这些查询并在单行中获取记录。两个查询中只有类型不同。 在此处输入图像描述

4

1 回答 1

2

我认为您只希望OR(or IN) 在WHERE子句中:

SELECT lp.lead_bucket_no, 
       SUM(case when p.product = 'S-400' then qty end) as S400,
       SUM(case when p.product = 'Dish Antenna' then qty end) as DishAntenna 
FROM lead_products lp INNER JOIN
     products p
     ON p.product_id = lp.product_id
WHERE type IN ('stock', 'order')
GROUP BY lead_bucket_no
ORDER BY lp.lead_bucket_no;

GROUP BY您还需要对不在子句中的表达式进行某种聚合。您可能还想按 聚合TYPE。目前尚不清楚您想要最终输出什么。

于 2017-03-24T11:13:13.087 回答