1
0

q30.sql 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. WITH customer_total_return AS
  2. (SELECT
  3. wr_returning_customer_sk AS ctr_customer_sk,
  4. ca_state AS ctr_state,
  5. sum(wr_return_amt) AS ctr_total_return
  6. FROM web_returns, date_dim, customer_address
  7. WHERE wr_returned_date_sk = d_date_sk
  8. AND d_year = 2002
  9. AND wr_returning_addr_sk = ca_address_sk
  10. GROUP BY wr_returning_customer_sk, ca_state)
  11. SELECT
  12. c_customer_id,
  13. c_salutation,
  14. c_first_name,
  15. c_last_name,
  16. c_preferred_cust_flag,
  17. c_birth_day,
  18. c_birth_month,
  19. c_birth_year,
  20. c_birth_country,
  21. c_login,
  22. c_email_address,
  23. c_last_review_date,
  24. ctr_total_return
  25. FROM customer_total_return ctr1, customer_address, customer
  26. WHERE ctr1.ctr_total_return > (SELECT avg(ctr_total_return) * 1.2
  27. FROM customer_total_return ctr2
  28. WHERE ctr1.ctr_state = ctr2.ctr_state)
  29. AND ca_address_sk = c_current_addr_sk
  30. AND ca_state = 'GA'
  31. AND ctr1.ctr_customer_sk = c_customer_sk
  32. ORDER BY c_customer_id, c_salutation, c_first_name, c_last_name, c_preferred_cust_flag
  33. , c_birth_day, c_birth_month, c_birth_year, c_birth_country, c_login, c_email_address
  34. , c_last_review_date, ctr_total_return
  35. LIMIT 100