Aggregate SQL query grouping by month

前端 未结 5 1782
忘掉有多难
忘掉有多难 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:24

    So as to Avoid conversion to strings, concatenations and conversion back to dates, use DATEADD() and DATEDIFF().

    SELECT
      DATEADD("m", DATEDIFF("m", 0, TransactionDate), 0) AS TransactionMonth,
      SUM(Usage)                                         AS TotalUsage
    FROM
      yourTable
    WHERE
      TransactionDate BETWEEN  AND 
    GROUP BY
      DATEADD("m", DATEDIFF("m", 0, TransactionDate), 0)
    ORDER BY
      DATEADD("m", DATEDIFF("m", 0, TransactionDate), 0)
    

提交回复
热议问题