q4.sql 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. WITH year_total AS (
  2. SELECT
  3. c_customer_id customer_id,
  4. c_first_name customer_first_name,
  5. c_last_name customer_last_name,
  6. c_preferred_cust_flag customer_preferred_cust_flag,
  7. c_birth_country customer_birth_country,
  8. c_login customer_login,
  9. c_email_address customer_email_address,
  10. d_year dyear,
  11. sum(((ss_ext_list_price - ss_ext_wholesale_cost - ss_ext_discount_amt) +
  12. ss_ext_sales_price) / 2) year_total,
  13. 's' sale_type
  14. FROM customer, store_sales, date_dim
  15. WHERE c_customer_sk = ss_customer_sk AND ss_sold_date_sk = d_date_sk
  16. GROUP BY c_customer_id,
  17. c_first_name,
  18. c_last_name,
  19. c_preferred_cust_flag,
  20. c_birth_country,
  21. c_login,
  22. c_email_address,
  23. d_year
  24. UNION ALL
  25. SELECT
  26. c_customer_id customer_id,
  27. c_first_name customer_first_name,
  28. c_last_name customer_last_name,
  29. c_preferred_cust_flag customer_preferred_cust_flag,
  30. c_birth_country customer_birth_country,
  31. c_login customer_login,
  32. c_email_address customer_email_address,
  33. d_year dyear,
  34. sum((((cs_ext_list_price - cs_ext_wholesale_cost - cs_ext_discount_amt) +
  35. cs_ext_sales_price) / 2)) year_total,
  36. 'c' sale_type
  37. FROM customer, catalog_sales, date_dim
  38. WHERE c_customer_sk = cs_bill_customer_sk AND cs_sold_date_sk = d_date_sk
  39. GROUP BY c_customer_id,
  40. c_first_name,
  41. c_last_name,
  42. c_preferred_cust_flag,
  43. c_birth_country,
  44. c_login,
  45. c_email_address,
  46. d_year
  47. UNION ALL
  48. SELECT
  49. c_customer_id customer_id,
  50. c_first_name customer_first_name,
  51. c_last_name customer_last_name,
  52. c_preferred_cust_flag customer_preferred_cust_flag,
  53. c_birth_country customer_birth_country,
  54. c_login customer_login,
  55. c_email_address customer_email_address,
  56. d_year dyear,
  57. sum((((ws_ext_list_price - ws_ext_wholesale_cost - ws_ext_discount_amt) + ws_ext_sales_price) /
  58. 2)) year_total,
  59. 'w' sale_type
  60. FROM customer, web_sales, date_dim
  61. WHERE c_customer_sk = ws_bill_customer_sk AND ws_sold_date_sk = d_date_sk
  62. GROUP BY c_customer_id,
  63. c_first_name,
  64. c_last_name,
  65. c_preferred_cust_flag,
  66. c_birth_country,
  67. c_login,
  68. c_email_address,
  69. d_year)
  70. SELECT
  71. t_s_secyear.customer_id,
  72. t_s_secyear.customer_first_name,
  73. t_s_secyear.customer_last_name,
  74. t_s_secyear.customer_preferred_cust_flag,
  75. t_s_secyear.customer_birth_country,
  76. t_s_secyear.customer_login,
  77. t_s_secyear.customer_email_address
  78. FROM year_total t_s_firstyear, year_total t_s_secyear, year_total t_c_firstyear,
  79. year_total t_c_secyear, year_total t_w_firstyear, year_total t_w_secyear
  80. WHERE t_s_secyear.customer_id = t_s_firstyear.customer_id
  81. AND t_s_firstyear.customer_id = t_c_secyear.customer_id
  82. AND t_s_firstyear.customer_id = t_c_firstyear.customer_id
  83. AND t_s_firstyear.customer_id = t_w_firstyear.customer_id
  84. AND t_s_firstyear.customer_id = t_w_secyear.customer_id
  85. AND t_s_firstyear.sale_type = 's'
  86. AND t_c_firstyear.sale_type = 'c'
  87. AND t_w_firstyear.sale_type = 'w'
  88. AND t_s_secyear.sale_type = 's'
  89. AND t_c_secyear.sale_type = 'c'
  90. AND t_w_secyear.sale_type = 'w'
  91. AND t_s_firstyear.dyear = 2001
  92. AND t_s_secyear.dyear = 2001 + 1
  93. AND t_c_firstyear.dyear = 2001
  94. AND t_c_secyear.dyear = 2001 + 1
  95. AND t_w_firstyear.dyear = 2001
  96. AND t_w_secyear.dyear = 2001 + 1
  97. AND t_s_firstyear.year_total > 0
  98. AND t_c_firstyear.year_total > 0
  99. AND t_w_firstyear.year_total > 0
  100. AND CASE WHEN t_c_firstyear.year_total > 0
  101. THEN t_c_secyear.year_total / t_c_firstyear.year_total
  102. ELSE NULL END
  103. > CASE WHEN t_s_firstyear.year_total > 0
  104. THEN t_s_secyear.year_total / t_s_firstyear.year_total
  105. ELSE NULL END
  106. AND CASE WHEN t_c_firstyear.year_total > 0
  107. THEN t_c_secyear.year_total / t_c_firstyear.year_total
  108. ELSE NULL END
  109. > CASE WHEN t_w_firstyear.year_total > 0
  110. THEN t_w_secyear.year_total / t_w_firstyear.year_total
  111. ELSE NULL END
  112. ORDER BY
  113. t_s_secyear.customer_id,
  114. t_s_secyear.customer_first_name,
  115. t_s_secyear.customer_last_name,
  116. t_s_secyear.customer_preferred_cust_flag,
  117. t_s_secyear.customer_birth_country,
  118. t_s_secyear.customer_login,
  119. t_s_secyear.customer_email_address
  120. LIMIT 100