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