我听取了一些构建sql语句的帮助.目前我有以下内容:
SELECT a,b,count(1) FROM table GROUP BY a,b ORDER BY a asc,count(1) DESC
每行显示每个分组a中所有唯一B的总和,并按字母顺序按A排序,然后按B的最高出现次数排序到最低.我真正想要的是能够按A的每个分组中的总数进行排序(将每一行视为小计).
解决方法
@H_502_9@SELECT a,COUNT(*) AS bcnt,( SELECT COUNT(*) FROM mytable mi WHERE mi.a = mo.a ) AS acnt FROM mytable mo GROUP BY a,b ORDER BY acnt DESC,bcnt DESC