我正在编写一个SQL Server查询来提取数据。SQL Server:Group By和Pivot
例如我的查询:
SELECT
DATEPART(Year, capDateTime) Year,
DATEPART(Month, capDateTime) Month,
SUM(CAST(amount AS MONEY)) [TotalAmount]
FROM
dbo.iso_main
GROUP BY
DATEPART(Year, capDateTime), DATEPART(Month, capDateTime)
ORDER BY
Year, Month
下面是结果:
现在,我一直在追问自己如何得到这样的结果:
|Month 7 | Month 8 |
+-----------+------------+
| 726800.00 | 2208400.00 |
我真的不在乎这一年,我只想要它返回一排o f个月。
只需检查[PIVOT](https://technet.microsoft.com/en-us/library/ms177410(v = sql.105).aspx) - 在这里实现起来会很简单。 –