q36.sql 985 B

123456789101112131415161718192021222324252627282930313233
  1. --!syntax_pg
  2. --TPC-DS Q36
  3. -- start query 1 in stream 0 using template ../query_templates/query36.tpl
  4. select
  5. sum(ss_net_profit)/sum(ss_ext_sales_price) as gross_margin
  6. ,i_category
  7. ,i_class
  8. ,grouping(i_category)+grouping(i_class) as lochierarchy
  9. ,rank() over (
  10. partition by grouping(i_category)+grouping(i_class),
  11. case when grouping(i_class) = 0 then i_category end
  12. order by sum(ss_net_profit)/sum(ss_ext_sales_price) asc) as rank_within_parent
  13. from
  14. plato.store_sales
  15. ,plato.date_dim d1
  16. ,plato.item
  17. ,plato.store
  18. where
  19. d1.d_year = 2000
  20. and d1.d_date_sk = ss_sold_date_sk
  21. and i_item_sk = ss_item_sk
  22. and s_store_sk = ss_store_sk
  23. and s_state in ('TN','TN','TN','TN',
  24. 'TN','TN','TN','TN')
  25. group by rollup(i_category,i_class)
  26. order by
  27. lochierarchy desc
  28. ,case when lochierarchy = 0 then i_category end
  29. ,rank_within_parent
  30. limit 100;
  31. -- end query 1 in stream 0 using template ../query_templates/query36.tpl