【发布时间】:2014-09-28 22:15:59
【问题描述】:
我在 SQL Server 2012 中有事务数据表,例如:
Month Type
----- -----
1 Paper
1 Paper
1 Mobile
2 Mobile
3 Paper
3 Mobile
如果我按月分组,我可以很容易地计算交易的发生次数:
SELECT Month, Count(*)
FROM Transaction
GROUP BY Month
如果我按月份和类型分组:
SELECT Month, Type, Count(Type) AS RowCount
FROM Transaction
GROUP BY Month,Type
我可以分解结果并获得每种类型的计数。但是,结果显示在不同的行中:
Month Type RowCount
------ ----- ---------
1 Paper 2
1 Mobile 1
2 Mobile 1
3 Paper 1
3 Mobile 1
现在我真正需要的是知道是否可以通过 ONE 查询生成以下结果:
Month Paper Mobile
------- ------- --------
1 2 1
2 0 1
3 1 1
有什么想法吗?
【问题讨论】:
-
纸和手机是你仅有的两种类型吗?
标签: sql-server tsql sql-server-2012