query70.sql 1.2 KB

1234567891011121314151617181920212223242526272829
  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 s.s_state in
  13. ( select s_state
  14. from (select s_state as s_state, sum(ss_net_profit),
  15. rank() over ( partition by s_state order by sum(ss_net_profit) desc) as ranking
  16. from store_sales, store, date_dim
  17. where d_month_seq between 1193 and 1193+11
  18. and date_dim.d_date_sk = store_sales.ss_sold_date_sk
  19. and store.s_store_sk = store_sales.ss_store_sk
  20. group by s_state
  21. ) tmp1
  22. where ranking <= 5
  23. )
  24. group by s_state,s_county with rollup
  25. order by
  26. lochierarchy desc
  27. ,case when lochierarchy = 0 then s_state end
  28. ,rank_within_parent
  29. limit 100;