q81.sql 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. WITH customer_total_return AS
  2. (SELECT
  3. cr_returning_customer_sk AS ctr_customer_sk,
  4. ca_state AS ctr_state,
  5. sum(cr_return_amt_inc_tax) AS ctr_total_return
  6. FROM catalog_returns, date_dim, customer_address
  7. WHERE cr_returned_date_sk = d_date_sk
  8. AND d_year = 2000
  9. AND cr_returning_addr_sk = ca_address_sk
  10. GROUP BY cr_returning_customer_sk, ca_state )
  11. SELECT
  12. c_customer_id,
  13. c_salutation,
  14. c_first_name,
  15. c_last_name,
  16. ca_street_number,
  17. ca_street_name,
  18. ca_street_type,
  19. ca_suite_number,
  20. ca_city,
  21. ca_county,
  22. ca_state,
  23. ca_zip,
  24. ca_country,
  25. ca_gmt_offset,
  26. ca_location_type,
  27. ctr_total_return
  28. FROM customer_total_return ctr1, customer_address, customer
  29. WHERE ctr1.ctr_total_return > (SELECT avg(ctr_total_return) * 1.2
  30. FROM customer_total_return ctr2
  31. WHERE ctr1.ctr_state = ctr2.ctr_state)
  32. AND ca_address_sk = c_current_addr_sk
  33. AND ca_state = 'GA'
  34. AND ctr1.ctr_customer_sk = c_customer_sk
  35. ORDER BY c_customer_id, c_salutation, c_first_name, c_last_name, ca_street_number, ca_street_name
  36. , ca_street_type, ca_suite_number, ca_city, ca_county, ca_state, ca_zip, ca_country, ca_gmt_offset
  37. , ca_location_type, ctr_total_return
  38. LIMIT 100