q80.sql 3.2 KB

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