I've got the fo开发者_StackOverflowllowing table:
purchases(id, item, user, price, time);
The time
field is a timestamp
.
I need a query that would return one row per month and that row would contain the sum of price
for each item in that month.
Try this:
SELECT MONTH(`time`) AS month, SUM(price) AS price
FROM your_table
GROUP BY MONTH(`time`)
If you have more than one year's data you may also want to include the year in your group by:
SELECT YEAR(`time`) AS year, MONTH(`time`) AS month, SUM(price) AS price
FROM your_table
GROUP BY YEAR(`time`), MONTH(`time`)
what about GROUP BY YEAR(DATE(time)) ASC, MONTH(DATE(time)) ASC
?
精彩评论