翻转降序值

时间:2017-01-10 09:28:38

标签: mysql sql

我想扭转我在这里的结果:

enter image description here

我知道我必须使用子查询,但它不能用作我的SQL代码:

SELECT MONTH(CaseTime) as 'Month', 
       COUNT(Case_ID) as 'Num' 
FROM `Case`
GROUP BY MONTH(CaseTime) 
ORDER BY CaseTime DESC LIMIT 6

MONTH(CaseTime)可能与别名冲突。

当前输出为:

Month / Num
5 / 1
4 / 1 
3 / 1
2 / 1
1 / 18
12 / 13

期望输出应为:

Month
12 / 13
1 / 18
2 / 1 
3 / 1
4 / 1
5 / 1

1 个答案:

答案 0 :(得分:0)

这应该有效

SELECT MONTH(CaseTime) as 'Month', 
       COUNT(Case_ID) as 'Num' 
FROM `Case`
GROUP BY MONTH(CaseTime) 
ORDER BY 1 DESC LIMIT 6

注意:您可能需要YEAR

的where子句
SELECT MONTH(CaseTime) as 'Month', 
       COUNT(Case_ID) as 'Num' 
FROM `Case`
WHERE YEAR(CaseTime) = YEAR(CURDATE())
GROUP BY MONTH(CaseTime) 
ORDER BY 1 DESC LIMIT 6