query85.sql 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 web_sales.ws_sold_date between '1998-01-01' and '1998-12-31'
  12. and cd1.cd_demo_sk = web_returns.wr_refunded_cdemo_sk
  13. and cd2.cd_demo_sk = web_returns.wr_returning_cdemo_sk
  14. and customer_address.ca_address_sk = web_returns.wr_refunded_addr_sk
  15. and reason.r_reason_sk = web_returns.wr_reason_sk
  16. and
  17. (
  18. (
  19. cd1.cd_marital_status = 'M'
  20. and
  21. cd1.cd_marital_status = cd2.cd_marital_status
  22. and
  23. cd1.cd_education_status = '4 yr Degree'
  24. and
  25. cd1.cd_education_status = cd2.cd_education_status
  26. and
  27. ws_sales_price between 100.00 and 150.00
  28. )
  29. or
  30. (
  31. cd1.cd_marital_status = 'D'
  32. and
  33. cd1.cd_marital_status = cd2.cd_marital_status
  34. and
  35. cd1.cd_education_status = 'Primary'
  36. and
  37. cd1.cd_education_status = cd2.cd_education_status
  38. and
  39. ws_sales_price between 50.00 and 100.00
  40. )
  41. or
  42. (
  43. cd1.cd_marital_status = 'U'
  44. and
  45. cd1.cd_marital_status = cd2.cd_marital_status
  46. and
  47. cd1.cd_education_status = 'Advanced Degree'
  48. and
  49. cd1.cd_education_status = cd2.cd_education_status
  50. and
  51. ws_sales_price between 150.00 and 200.00
  52. )
  53. )
  54. and
  55. (
  56. (
  57. ca_country = 'United States'
  58. and
  59. ca_state in ('KY', 'GA', 'NM')
  60. and ws_net_profit between 100 and 200
  61. )
  62. or
  63. (
  64. ca_country = 'United States'
  65. and
  66. ca_state in ('MT', 'OR', 'IN')
  67. and ws_net_profit between 150 and 300
  68. )
  69. or
  70. (
  71. ca_country = 'United States'
  72. and
  73. ca_state in ('WI', 'MO', 'WV')
  74. and ws_net_profit between 50 and 250
  75. )
  76. )
  77. group by r_reason_desc
  78. order by r, wq, ref, fee
  79. limit 100;