表结构如上图,其中Sequence中的值为0,1,2。
现在我想要查出每个WorkNo
当Sequence=‘0’ Group By WorkNo → Count(Sequence) ,
Sequence=‘1’ Group By WorkNo → Count(Sequence),
Sequence=‘2’ Group By WorkNo → Count(Sequence),
的次数。
希望得到的结果如下图。
这样一个结果。
------解决方案--------------------
select A=WorkNo,
B=SUM(case when Sequence='0' then 1 else 0 end),
C=SUM(case when Sequence='1' then 1 else 0 end),
D=SUM(case when Sequence='2' then 1 else 0 end)
from tableA
group by WorkNo