query77.sql 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. -- start query 1 in stream 0 using template query77.tpl and seed 1819994127
  2. with ss as
  3. (select s_store_sk,
  4. sum(ss_ext_sales_price) as sales,
  5. sum(ss_net_profit) as profit
  6. from store_sales,
  7. date_dim,
  8. store
  9. where ss_sold_date_sk = d_date_sk
  10. and d_date between cast('2000-08-16' as date)
  11. and (cast('2000-08-16' as date) + 30 days)
  12. and ss_store_sk = s_store_sk
  13. group by s_store_sk)
  14. ,
  15. sr as
  16. (select s_store_sk,
  17. sum(sr_return_amt) as returns,
  18. sum(sr_net_loss) as profit_loss
  19. from store_returns,
  20. date_dim,
  21. store
  22. where sr_returned_date_sk = d_date_sk
  23. and d_date between cast('2000-08-16' as date)
  24. and (cast('2000-08-16' as date) + 30 days)
  25. and sr_store_sk = s_store_sk
  26. group by s_store_sk),
  27. cs as
  28. (select cs_call_center_sk,
  29. sum(cs_ext_sales_price) as sales,
  30. sum(cs_net_profit) as profit
  31. from catalog_sales,
  32. date_dim
  33. where cs_sold_date_sk = d_date_sk
  34. and d_date between cast('2000-08-16' as date)
  35. and (cast('2000-08-16' as date) + 30 days)
  36. group by cs_call_center_sk
  37. ),
  38. cr as
  39. (select cr_call_center_sk,
  40. sum(cr_return_amount) as returns,
  41. sum(cr_net_loss) as profit_loss
  42. from catalog_returns,
  43. date_dim
  44. where cr_returned_date_sk = d_date_sk
  45. and d_date between cast('2000-08-16' as date)
  46. and (cast('2000-08-16' as date) + 30 days)
  47. group by cr_call_center_sk
  48. ),
  49. ws as
  50. ( select wp_web_page_sk,
  51. sum(ws_ext_sales_price) as sales,
  52. sum(ws_net_profit) as profit
  53. from web_sales,
  54. date_dim,
  55. web_page
  56. where ws_sold_date_sk = d_date_sk
  57. and d_date between cast('2000-08-16' as date)
  58. and (cast('2000-08-16' as date) + 30 days)
  59. and ws_web_page_sk = wp_web_page_sk
  60. group by wp_web_page_sk),
  61. wr as
  62. (select wp_web_page_sk,
  63. sum(wr_return_amt) as returns,
  64. sum(wr_net_loss) as profit_loss
  65. from web_returns,
  66. date_dim,
  67. web_page
  68. where wr_returned_date_sk = d_date_sk
  69. and d_date between cast('2000-08-16' as date)
  70. and (cast('2000-08-16' as date) + 30 days)
  71. and wr_web_page_sk = wp_web_page_sk
  72. group by wp_web_page_sk)
  73. select channel
  74. , id
  75. , sum(sales) as sales
  76. , sum(returns) as returns
  77. , sum(profit) as profit
  78. from
  79. (select 'store channel' as channel
  80. , ss.s_store_sk as id
  81. , sales
  82. , coalesce(returns, 0) as returns
  83. , (profit - coalesce(profit_loss,0)) as profit
  84. from ss left join sr
  85. on ss.s_store_sk = sr.s_store_sk
  86. union all
  87. select 'catalog channel' as channel
  88. , cs_call_center_sk as id
  89. , sales
  90. , returns
  91. , (profit - profit_loss) as profit
  92. from cs
  93. , cr
  94. union all
  95. select 'web channel' as channel
  96. , ws.wp_web_page_sk as id
  97. , sales
  98. , coalesce(returns, 0) returns
  99. , (profit - coalesce(profit_loss,0)) as profit
  100. from ws left join wr
  101. on ws.wp_web_page_sk = wr.wp_web_page_sk
  102. ) x
  103. group by rollup (channel, id)
  104. order by channel
  105. ,id
  106. limit 100;
  107. -- end query 1 in stream 0 using template query77.tpl