Oracle
 sql >> Database >  >> RDS >> Oracle

Limita la query SQL solo ai primi due conteggi per gruppo

SELECT
   State,
   flv,
   total
FROM (SELECT
         ROW_NUMBER() OVER ( PARTITION BY state ORDER BY count(INITCAP(TRIM(flavor))) DESC ) RowNumber,
         State,
         INITCAP(TRIM(flavor)) flv,
         count(INITCAP(TRIM(flavor))) total
      FROM favorite_flavor
      GROUP BY state, INITCAP(TRIM(flavor))
      ) dt
WHERE RowNumber <= 2
ORDER BY state ASC, total DESC