我现在有一个sql如下
select a,count(a) count_num,days from
(select a,case when days < 60 then '库存小于60天' WHEN DAYS <=90 AND DAYS >=60 THEN '60-90天' ELSE '大于90天' end days fr om
(select a,
(格式化) DAYS
from A
where sn
.STORAGETYPE
not in('S01','918','920')
) a)b
group by a,DAYS
简化
select a,b,count(c) from A group by a,b order by ?
解释:也就是说我现在想要对两个数来分组排序,但是排序的结果想要按其中某个字段分组后count的值去排序,怎么实现?
外面再套一层,然后里面查询出来的count作为列,外面根据这个列进行排序。
1、你最好举个样例数据和排序结果来说明具体需要的排序规则,按目前的描述来看,你似乎需要按count(c)来排序?
select a,b,count(c) from A group by a,b order by 3
-- 或
select a,b,count(c) from A group by a,b order by 3, a
-- 或
select a,b,count(c) from A group by a,b order by a, 3
1.
# 查询学号、时间、课程数量并且按照学号降序排列
select sc.sno ,sc.time,count(sc.cid)as count from sc group by sc.sno,sc.time order by sc.sno desc
# 查询学号、时间、课程数量并且按照课程数量降序排列
select sc.sno ,sc.time,count(sc.cid)as count from sc group by sc.sno,sc.time order by count desc