计算状态类型并将总计放在一行中

时间:2011-09-24 14:37:30

标签: tsql

我有以下结果集

Status, Name
0, Type0
0, Type0
1, Type1
0, Type0
2, Type2

如何将类型的数量相加并使其看起来像

Type0,Type1,Type2
3,1,1

1 个答案:

答案 0 :(得分:1)

SELECT 
     COUNT(CASE WHEN Name='Type0' THEN 1 END) AS Type0,
     COUNT(CASE WHEN Name='Type1' THEN 1 END) AS Type1,
     COUNT(CASE WHEN Name='Type2' THEN 1 END) AS Type2
FROM YourTable