大家好,通过根据日期对销售额进行分组,查询显示每个月的销售额。
但我想在每个月的销售额旁边显示。我尝试了以下查询,它将只返回销售额。但没有几个月。
SELECT
CASE
WHEN created_at BETWEEN "2012-01-01" and "2012-01-31" THEN "Jan"
WHEN created_at BETWEEN "2012-02-01" and "2012-02-28" THEN "Feb"
WHEN created_at BETWEEN "2012-03-01" and "2012-03-31" THEN "Mar"
WHEN created_at BETWEEN "2012-04-01" and "2012-04-30" THEN "April"
WHEN created_at BETWEEN "2012-05-01" and "2012-05-31" THEN "May"
WHEN created_at BETWEEN "2012-06-01" and "2012-06-30" THEN "June"
WHEN created_at BETWEEN "2012-07-01" and "2012-07-31" THEN "July"
WHEN created_at BETWEEN "2012-08-01" and "2012-08-31" THEN "Aug"
WHEN created_at BETWEEN "2012-09-01" and "2012-09-30" THEN "Sep"
WHEN created_at BETWEEN "2012-10-01" and "2012-10-31" THEN "Oct"
WHEN created_at BETWEEN "2012-11-01" and "2012-11-30" THEN "Nov"
ELSE "Dec"
END AS "month" , SUM(grand_total) AS total
FROM sales
WHERE created_at >="2012-01-01" AND created_at <="2013-01-01"
GROUP BY TIMESTAMPDIFF(MONTH, "2012-01-01" + INTERVAL 1 DAY, created_at)
我如何在销售旁边显示月份,如下所示。
+------+-------+
| Month| Total |
+------+-------+
| Jan | 1200 |
| Feb | 5555 |
| Mar | 1235 |
| Apr | 1299 |
+------+-------+
提前致谢。