如何获得每个数字的计数?

时间:2019-01-28 20:49:08

标签: sql sql-server

我有一张这样的桌子

Name  Value
test  1
test  1
test  2
test  3

如何获得这样的东西?

Name 1  2  3
test 2  1  1

1 个答案:

答案 0 :(得分:1)

检查此

select 
  name,
  sum(IIF(Value=1, 1,0)) as c1,
  sum(IIF(Value=2, 1,0)) as c2,
  sum(IIF(Value=3, 1,0)) as c2
from t
group by name