query80.sql 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. with ssr as
  2. (select s_store_id as store_id,
  3. sum(ss_ext_sales_price) as sales,
  4. sum(coalesce(sr_return_amt, 0)) as returns,
  5. sum(ss_net_profit - coalesce(sr_net_loss, 0)) as profit
  6. from store_sales left outer join store_returns on
  7. (ss_item_sk = sr_item_sk and ss_ticket_number = sr_ticket_number),
  8. date_dim,
  9. store,
  10. item,
  11. promotion
  12. where ss_sold_date_sk = d_date_sk
  13. and d_date between cast('1998-08-04' as date)
  14. and (cast('1998-09-04' as date))
  15. and ss_store_sk = s_store_sk
  16. and ss_item_sk = i_item_sk
  17. and i_current_price > 50
  18. and ss_promo_sk = p_promo_sk
  19. and p_channel_tv = 'N'
  20. group by s_store_id)
  21. ,
  22. csr as
  23. (select cp_catalog_page_id as catalog_page_id,
  24. sum(cs_ext_sales_price) as sales,
  25. sum(coalesce(cr_return_amount, 0)) as returns,
  26. sum(cs_net_profit - coalesce(cr_net_loss, 0)) as profit
  27. from catalog_sales left outer join catalog_returns on
  28. (cs_item_sk = cr_item_sk and cs_order_number = cr_order_number),
  29. date_dim,
  30. catalog_page,
  31. item,
  32. promotion
  33. where cs_sold_date_sk = d_date_sk
  34. and d_date between cast('1998-08-04' as date)
  35. and (cast('1998-09-04' as date))
  36. and cs_catalog_page_sk = cp_catalog_page_sk
  37. and cs_item_sk = i_item_sk
  38. and i_current_price > 50
  39. and cs_promo_sk = p_promo_sk
  40. and p_channel_tv = 'N'
  41. group by cp_catalog_page_id)
  42. ,
  43. wsr as
  44. (select web_site_id,
  45. sum(ws_ext_sales_price) as sales,
  46. sum(coalesce(wr_return_amt, 0)) as returns,
  47. sum(ws_net_profit - coalesce(wr_net_loss, 0)) as profit
  48. from web_sales left outer join web_returns on
  49. (ws_item_sk = wr_item_sk and ws_order_number = wr_order_number),
  50. date_dim,
  51. web_site,
  52. item,
  53. promotion
  54. where ws_sold_date_sk = d_date_sk
  55. and d_date between cast('1998-08-04' as date)
  56. and (cast('1998-09-04' as date))
  57. and ws_web_site_sk = web_site_sk
  58. and ws_item_sk = i_item_sk
  59. and i_current_price > 50
  60. and ws_promo_sk = p_promo_sk
  61. and p_channel_tv = 'N'
  62. group by web_site_id)
  63. select channel
  64. , id
  65. , sum(sales) as sales
  66. , sum(returns) as returns
  67. , sum(profit) as profit
  68. from
  69. (select 'store channel' as channel
  70. , concat('store', store_id) as id
  71. , sales
  72. , returns
  73. , profit
  74. from ssr
  75. union all
  76. select 'catalog channel' as channel
  77. , concat('catalog_page', catalog_page_id) as id
  78. , sales
  79. , returns
  80. , profit
  81. from csr
  82. union all
  83. select 'web channel' as channel
  84. , concat('web_site', web_site_id) as id
  85. , sales
  86. , returns
  87. , profit
  88. from wsr
  89. ) x
  90. group by channel, id with rollup
  91. order by channel
  92. ,id
  93. limit 100;