Aggregate SQL query grouping by month

前端 未结 5 1787
忘掉有多难
忘掉有多难 2021-01-20 06:52

I have a database of Transactions (Access 2007) that are recorded in hourly, daily and monthly intervals. I would like to view them in a meaningful way (instead of hour-by-

5条回答
  •  难免孤独
    2021-01-20 07:37

    It seems that you would need to group by both the month and the year. Otherwise, you'll have January 2010 and January 2011 combined:

    SELECT YEAR(TransactionDate), MONTH(TransactionDate), SUM(Usage)
    FROM YourTable
    WHERE (TransactionDate Between [Some Start Date] AND[Some End Date])
    GROUP BY YEAR(TransactionDate), MONTH(TransactionDate)
    ORDER BY YEAR(Created), MONTH(Created)
    

    I don't know if your version of SQL has the MONTH and YEAR functions, so you may have to use DATEPART.

提交回复
热议问题