是否可以在同一声明中分组并排序?以下语句以随机顺序输出月份。它会生成正确的数字等,但我想将这些月份显示为1月,2月,3月,4月等顺序
SELECT coalesce(date_format(TRANSACTION_DATE, '%M'), 'Grand Total') AS MONTH,
ROUND(SUM(SALES_AMOUNT),2) TOTAL, SUM(QUANTITY) AS TOTAL_QUANTITY
FROM SALES
WHERE YEAR(TRANSACTION_DATE) = 2018
GROUP BY month WITH ROLLUP;
April 30826.68 476
August 39703.85 619
December 36986.16 515
February 34531.15 463
January 45163.22 596
July 37640.08 506
June 37284.25 496
March 44194.98 621
May 39016.04 576
November 39161.90 521
October 34447.61 485
September 34958.94 498
453914.86 6372
答案 0 :(得分:1)
是否可以在同一声明中分组并排序? 是的,这可能只是你需要解决它。
如果您可以从此查询中删除汇总,则可以执行以下排序:
SELECT coalesce(date_format(TRANSACTION_DATE, '%M'), 'Grand Total') AS MONTH, ROUND(SUM(SALES_AMOUNT),2) TOTAL, SUM(QUANTITY) AS TOTAL_QUANTITY FROM SALES WHERE YEAR(TRANSACTION_DATE) = 2018 GROUP BY month order by month(TRANSACTION_DATE) asc;
答案 1 :(得分:0)
This should order the months in the expected order:
SELECT coalesce(date_format(TRANSACTION_DATE, '%M'), 'Grand Total') AS MONTH,
ROUND(SUM(SALES_AMOUNT),2) TOTAL, SUM(QUANTITY) AS TOTAL_QUANTITY
FROM SALES
WHERE YEAR(TRANSACTION_DATE) = 2018
GROUP BY month WITH ROLLUP
ORDER BY date_format(TRANSACTION_DATE, '%m');
Notice the lower case m
in the order by. That's for the numerical representation of the month.