query49.sql 3.7 KB

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