SQL ORDER BY total inside GROUP BY

I listened to any help in building the SQL stat. I currently have the following:

SELECT a, b, count(1) FROM table GROUP BY a, b ORDER BY a asc, count(1) DESC 

Each row displays the sum of all unique Bs in each group a and orders alphabetically, and then by the highest occurrence of B at the lowest. I would like that I could sort by the sum in each group A (consider each row as an intermediate result).

+4
source share
1 answer
 SELECT a, b, 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 
+4
source

Source: https://habr.com/ru/post/1300880/


All Articles