Control Freak
Control Freak

Reputation: 13233

Display a yearly summary by month in SQL

I have a table that has a date/time field and am trying to figure out how to run a report so that I can view the sum of the amount between each month separately in the 2012 year.

The table has 2 fields, Amount and TimeStamp, and I'm trying to return a report like this:

etc etc.

Anyone have any ideas how to accomplish this easily in SQL Server? I want to avoid writing a seperate query for each individual month.

Upvotes: 0

Views: 1761

Answers (1)

OneRealWinner
OneRealWinner

Reputation: 677

This can be solved by using the MONTH and YEAR Functions on SQL.

SELECT
    SUM(Amount) as [Amount]
    ,MONTH(TimeStamp) as [Month]
    ,YEAR(TimeStamp) as [Year]
FROM
    [MyTable]
GROUP BY
     MONTH(TimeStamp)
     ,YEAR(TimeStamp)

Upvotes: 1

Related Questions