2013-07-04 105 views
3

我在SQL Server 2012中一個數據庫,並有一個表像如何計算一年中每月的總金額?

ID | Date(date type) | Amount(Numeric) 
    1  3.4.2013   16.00 
    1  12.4.2013   13.00 
    1  2.5.2013   9.50 
    1  18.5.2013   10.00 

我需要在一年內合計金額爲每月。

例如:

ID | Month | TotalAmount 
    1  1   0.00 
    ... 
    1  4   29.00 
    1  5   19.50  

餘因子評分需要確定在蛾的天數和我創建其在determine the number of days描述的功能。有用。之後,我試圖比較兩個日期(日期類型)。有一些例子,但所有的日期時間。所以我很困難。這是一個錯誤的方式?我怎樣才能做到這一點 ?請給我任何建議。謝謝。

回答

6

我覺得你只是想匯聚:

select id, month(date) as "month", sum(amount) as TotalAmount 
from t 
where year(date) = 2013 
group by id, month(date)