q49.sql 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. --!syntax_pg
  2. --TPC-DS Q49
  3. -- start query 1 in stream 0 using template ../query_templates/query49.tpl
  4. select channel, item, return_ratio, return_rank, currency_rank from
  5. (select
  6. 'web' as channel
  7. ,web.item
  8. ,web.return_ratio
  9. ,web.return_rank
  10. ,web.currency_rank
  11. from (
  12. select
  13. item
  14. ,return_ratio
  15. ,currency_ratio
  16. ,rank() over (order by return_ratio) as return_rank
  17. ,rank() over (order by currency_ratio) as currency_rank
  18. from
  19. ( select ws.ws_item_sk as item
  20. ,(cast(sum(coalesce(wr.wr_return_quantity,0)) as decimal(15,4))/
  21. cast(sum(coalesce(ws.ws_quantity,0)) as decimal(15,4) )) as return_ratio
  22. ,(cast(sum(coalesce(wr.wr_return_amt,0::numeric)) as decimal(15,4))/
  23. cast(sum(coalesce(ws.ws_net_paid,0::numeric)) as decimal(15,4) )) as currency_ratio
  24. from
  25. plato.web_sales ws left outer join plato.web_returns wr
  26. on (ws.ws_order_number = wr.wr_order_number and
  27. ws.ws_item_sk = wr.wr_item_sk)
  28. ,plato.date_dim
  29. where
  30. wr.wr_return_amt > 10000::numeric
  31. and ws.ws_net_profit > 1::numeric
  32. and ws.ws_net_paid > 0::numeric
  33. and ws.ws_quantity > 0
  34. and ws_sold_date_sk = d_date_sk
  35. and d_year = 2000
  36. and d_moy = 12
  37. group by ws.ws_item_sk
  38. ) in_web
  39. ) web
  40. where
  41. (
  42. web.return_rank <= 10
  43. or
  44. web.currency_rank <= 10
  45. )
  46. union
  47. select
  48. 'catalog' as channel
  49. ,catalog.item
  50. ,catalog.return_ratio
  51. ,catalog.return_rank
  52. ,catalog.currency_rank
  53. from (
  54. select
  55. item
  56. ,return_ratio
  57. ,currency_ratio
  58. ,rank() over (order by return_ratio) as return_rank
  59. ,rank() over (order by currency_ratio) as currency_rank
  60. from
  61. ( select
  62. cs.cs_item_sk as item
  63. ,(cast(sum(coalesce(cr.cr_return_quantity,0)) as decimal(15,4))/
  64. cast(sum(coalesce(cs.cs_quantity,0)) as decimal(15,4) )) as return_ratio
  65. ,(cast(sum(coalesce(cr.cr_return_amount,0::numeric)) as decimal(15,4))/
  66. cast(sum(coalesce(cs.cs_net_paid,0::numeric)) as decimal(15,4) )) as currency_ratio
  67. from
  68. plato.catalog_sales cs left outer join plato.catalog_returns cr
  69. on (cs.cs_order_number = cr.cr_order_number and
  70. cs.cs_item_sk = cr.cr_item_sk)
  71. ,plato.date_dim
  72. where
  73. cr.cr_return_amount > 10000::numeric
  74. and cs.cs_net_profit > 1::numeric
  75. and cs.cs_net_paid > 0::numeric
  76. and cs.cs_quantity > 0
  77. and cs_sold_date_sk = d_date_sk
  78. and d_year = 2000
  79. and d_moy = 12
  80. group by cs.cs_item_sk
  81. ) in_cat
  82. ) catalog
  83. where
  84. (
  85. catalog.return_rank <= 10
  86. or
  87. catalog.currency_rank <=10
  88. )
  89. union
  90. select
  91. 'store' as channel
  92. ,store.item
  93. ,store.return_ratio
  94. ,store.return_rank
  95. ,store.currency_rank
  96. from (
  97. select
  98. item
  99. ,return_ratio
  100. ,currency_ratio
  101. ,rank() over (order by return_ratio) as return_rank
  102. ,rank() over (order by currency_ratio) as currency_rank
  103. from
  104. ( select sts.ss_item_sk as item
  105. ,(cast(sum(coalesce(sr.sr_return_quantity,0)) as decimal(15,4))/cast(sum(coalesce(sts.ss_quantity,0)) as decimal(15,4) )) as return_ratio
  106. ,(cast(sum(coalesce(sr.sr_return_amt,0::numeric)) as decimal(15,4))/cast(sum(coalesce(sts.ss_net_paid,0::numeric)) as decimal(15,4) )) as currency_ratio
  107. from
  108. plato.store_sales sts left outer join plato.store_returns sr
  109. on (sts.ss_ticket_number = sr.sr_ticket_number and sts.ss_item_sk = sr.sr_item_sk)
  110. ,plato.date_dim
  111. where
  112. sr.sr_return_amt > 10000::numeric
  113. and sts.ss_net_profit > 1::numeric
  114. and sts.ss_net_paid > 0::numeric
  115. and sts.ss_quantity > 0
  116. and ss_sold_date_sk = d_date_sk
  117. and d_year = 2000
  118. and d_moy = 12
  119. group by sts.ss_item_sk
  120. ) in_store
  121. ) store
  122. where (
  123. store.return_rank <= 10
  124. or
  125. store.currency_rank <= 10
  126. )
  127. ) a
  128. order by 1,4,5,2
  129. limit 100;
  130. -- end query 1 in stream 0 using template ../query_templates/query49.tpl