My problem statement is like
"Find top 2 districts per state with the highest population"
data is like
My expected output is
I tried this with lot of queries and sub-queries but results in SQL error with the sub query
Can anyone help me with getting this result?
Thanks in advance.
Queries I tried
- Select state_name, (select concat_ws(',', collect_set(dist_name as string)) from population where state_name = state_name group by state order by population desc 2)
from population group by state_name
- select
state_name, concat_ws(',', collect_set(cast(dist_name as string)))
from population where population.dist_name in (select dist_name from ( select dist_name , max(b.population) as total from population b where state_name = b.state_name group by b.dist_name , b.dist_name order by total desc limit 2) as dist_name ) group by state_name