简体   繁体   English

如何计算同一列中的两个不同值并将这些计数输出到两个不同的列

[英]How to count two different values in the same column and output those counts to two different columns

So I have this code already.所以我已经有了这个代码。

select
Item,
count(WORK_TYPE) AS 'Capacity Replen'
from WORK_INSTRUCTION
where WORK_TYPE = 'Replen - Capacity'
Group by ITEM

Which outputs this:输出这个:

Item    Capacity Replen
E000191208  3
E000191904  2
E000328017  2
E000397711  2

I need to be able to count a different Work_Type as well and output that count to the associated item.我还需要能够计算不同的 Work_Type 并将该计数输出到相关项目。

I think you're looking for conditional aggregation.我认为您正在寻找条件聚合。

SELECT 
   Item
  ,SUM( CASE WHEN WORK_TYPE= 'Replen - Capacity' THEN 1 ELSE 0 END) AS 'Capacity Replen'
  ,SUM( CASE WHEN WORK_TYPE= 'Some Other Criteria' THEN 1 ELSE 0 END) AS 'Some Other Column Name'  
FROM WORK_INSTRUCTION
WHERE WORK_TYPE IN ('Replen - Capacity','Some Other Criteria')
GROUP BY ITEM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM