如何找出特定月份最畅销的书籍类别

时间:2018-09-03 20:03:17

标签: mysql

我正在尝试计算特定月份(例如一月份)销售量最大(书籍)的类别。我的payment_date列为日期时间格式。它说我的SQL语法有以下查询错误

SELECT category_id, SUM(payment_amount) AS Total FROM Payments GROUP BY category_id WHERE MONTH(payment_date) = 01 ORDER BY SUM(payment_amount) DESC LIMIT 1

1 个答案:

答案 0 :(得分:0)

该语句的GROUP BY部分需要放在WHERE子句之后,因此您的查询应该是:

SELECT category_id, SUM(payment_amount) AS Total FROM Payments WHERE MONTH(payment_date) = 1 GROUP BY category_id ORDER BY SUM(payment_amount) DESC LIMIT 1