SQl查询按月/年汇总金额

时间:2014-01-07 14:38:02

标签: sql-server

我有一个像这样的表“TABLE”:

Date(datatime)
Paid(int)

我每月有多个“付费”金额。 我想总结每月/每年的付费金额。

到目前为止,这是我尝试的内容,但是我在EXTRACT和MONTH中遇到了错误,但是这些年来我很难完成它。

SELECT 
     EXTRACT(MONTH FROM Period) AS reference_month
     , SUM(Paid) AS monthly_payments 
FROM Paid 
GROUP BY EXTRACT(MONTH FROM Period) 
ORDER BY EXTRACT(MONTH FROM Period)

我对此并不是很方便,我真的很感激一些帮助。

2 个答案:

答案 0 :(得分:10)

select year(date) as y, month(date) as m, sum(paid) as p
from table
group by year(date), month(date)

答案 1 :(得分:2)

SELECT YEAR([Date]) AS [Year], MONTH([Date]) AS [Month], SUM(Paid) AS Total
FROM TABLE_NAME
GROUP BY YEAR([Date]), MONTH([Date]) 

当您使用sql server []命名对象时,需要在对象名key words周围使用方括号。