query85.sql 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. select substr(r_reason_desc,1,20) as r
  2. ,avg(ws_quantity) wq
  3. ,avg(wr_refunded_cash) ref
  4. ,avg(wr_fee) fee
  5. from web_sales, web_returns, web_page, customer_demographics cd1,
  6. customer_demographics cd2, customer_address, date_dim, reason
  7. where web_sales.ws_web_page_sk = web_page.wp_web_page_sk
  8. and web_sales.ws_item_sk = web_returns.wr_item_sk
  9. and web_sales.ws_order_number = web_returns.wr_order_number
  10. and web_sales.ws_sold_date_sk = date_dim.d_date_sk and d_year = 1998
  11. and cd1.cd_demo_sk = web_returns.wr_refunded_cdemo_sk
  12. and cd2.cd_demo_sk = web_returns.wr_returning_cdemo_sk
  13. and customer_address.ca_address_sk = web_returns.wr_refunded_addr_sk
  14. and reason.r_reason_sk = web_returns.wr_reason_sk
  15. and
  16. (
  17. (
  18. cd1.cd_marital_status = 'M'
  19. and
  20. cd1.cd_marital_status = cd2.cd_marital_status
  21. and
  22. cd1.cd_education_status = '4 yr Degree'
  23. and
  24. cd1.cd_education_status = cd2.cd_education_status
  25. and
  26. ws_sales_price between 100.00 and 150.00
  27. )
  28. or
  29. (
  30. cd1.cd_marital_status = 'D'
  31. and
  32. cd1.cd_marital_status = cd2.cd_marital_status
  33. and
  34. cd1.cd_education_status = 'Primary'
  35. and
  36. cd1.cd_education_status = cd2.cd_education_status
  37. and
  38. ws_sales_price between 50.00 and 100.00
  39. )
  40. or
  41. (
  42. cd1.cd_marital_status = 'U'
  43. and
  44. cd1.cd_marital_status = cd2.cd_marital_status
  45. and
  46. cd1.cd_education_status = 'Advanced Degree'
  47. and
  48. cd1.cd_education_status = cd2.cd_education_status
  49. and
  50. ws_sales_price between 150.00 and 200.00
  51. )
  52. )
  53. and
  54. (
  55. (
  56. ca_country = 'United States'
  57. and
  58. ca_state in ('KY', 'GA', 'NM')
  59. and ws_net_profit between 100 and 200
  60. )
  61. or
  62. (
  63. ca_country = 'United States'
  64. and
  65. ca_state in ('MT', 'OR', 'IN')
  66. and ws_net_profit between 150 and 300
  67. )
  68. or
  69. (
  70. ca_country = 'United States'
  71. and
  72. ca_state in ('WI', 'MO', 'WV')
  73. and ws_net_profit between 50 and 250
  74. )
  75. )
  76. group by r_reason_desc
  77. order by r, wq, ref, fee
  78. limit 100;