query70.sql 1.3 KB

12345678910111213141516171819202122232425262728293031
  1. select
  2. sum(ss_net_profit) as total_sum
  3. ,s_state
  4. ,s_county
  5. ,grouping__id as lochierarchy
  6. , rank() over(partition by grouping__id, case when grouping__id == 2 then s_state end order by sum(ss_net_profit)) as rank_within_parent
  7. from
  8. store_sales ss join date_dim d1 on d1.d_date_sk = ss.ss_sold_date_sk
  9. join store s on s.s_store_sk = ss.ss_store_sk
  10. where
  11. d1.d_month_seq between 1193 and 1193+11
  12. and ss_sold_date between '1999-06-01' and '2000-05-31'
  13. and s.s_state in
  14. ( select s_state
  15. from (select s_state as s_state, sum(ss_net_profit),
  16. rank() over ( partition by s_state order by sum(ss_net_profit) desc) as ranking
  17. from store_sales, store, date_dim
  18. where d_month_seq between 1193 and 1193+11
  19. and date_dim.d_date_sk = store_sales.ss_sold_date_sk
  20. and store.s_store_sk = store_sales.ss_store_sk
  21. and ss_sold_date between '1999-06-01' and '2000-05-31'
  22. group by s_state
  23. ) tmp1
  24. where ranking <= 5
  25. )
  26. group by s_state,s_county with rollup
  27. order by
  28. lochierarchy desc
  29. ,case when lochierarchy = 0 then s_state end
  30. ,rank_within_parent
  31. limit 100;