query77.sql 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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('1998-08-04' as date)
  11. and (cast('1998-08-04' as date) + interval '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('1998-08-04' as date)
  24. and (cast('1998-08-04' as date) + interval '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('1998-08-04' as date)
  35. and (cast('1998-08-04' as date) + interval '30' days)
  36. group by cs_call_center_sk
  37. ),
  38. cr as
  39. (select
  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('1998-08-04' as date)
  46. and (cast('1998-08-04' as date) + interval '30' days)
  47. ),
  48. ws as
  49. ( select wp_web_page_sk,
  50. sum(ws_ext_sales_price) as sales,
  51. sum(ws_net_profit) as profit
  52. from web_sales,
  53. date_dim,
  54. web_page
  55. where ws_sold_date_sk = d_date_sk
  56. and d_date between cast('1998-08-04' as date)
  57. and (cast('1998-08-04' as date) + interval '30' days)
  58. and ws_web_page_sk = wp_web_page_sk
  59. group by wp_web_page_sk),
  60. wr as
  61. (select wp_web_page_sk,
  62. sum(wr_return_amt) as returns,
  63. sum(wr_net_loss) as profit_loss
  64. from web_returns,
  65. date_dim,
  66. web_page
  67. where wr_returned_date_sk = d_date_sk
  68. and d_date between cast('1998-08-04' as date)
  69. and (cast('1998-08-04' as date) + interval '30' days)
  70. and wr_web_page_sk = wp_web_page_sk
  71. group by wp_web_page_sk)
  72. select channel
  73. , id
  74. , sum(sales) as sales
  75. , sum(returns) as returns
  76. , sum(profit) as profit
  77. from
  78. (select 'store channel' as channel
  79. , ss.s_store_sk as id
  80. , sales
  81. , coalesce(returns, 0) as returns
  82. , (profit - coalesce(profit_loss,0)) as profit
  83. from ss left join sr
  84. on ss.s_store_sk = sr.s_store_sk
  85. union all
  86. select 'catalog channel' as channel
  87. , cs_call_center_sk as id
  88. , sales
  89. , returns
  90. , (profit - profit_loss) as profit
  91. from cs
  92. , cr
  93. union all
  94. select 'web channel' as channel
  95. , ws.wp_web_page_sk as id
  96. , sales
  97. , coalesce(returns, 0) returns
  98. , (profit - coalesce(profit_loss,0)) as profit
  99. from ws left join wr
  100. on ws.wp_web_page_sk = wr.wp_web_page_sk
  101. ) x
  102. group by rollup (channel, id)
  103. order by channel
  104. ,id
  105. limit 100;
  106. -- end query 1 in stream 0 using template query77.tpl