query73.sql 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. select c_last_name
  2. ,c_first_name
  3. ,c_salutation
  4. ,c_preferred_cust_flag
  5. ,ss_ticket_number
  6. ,cnt from
  7. (select ss_ticket_number
  8. ,ss_customer_sk
  9. ,count(*) cnt
  10. from store_sales,date_dim,store,household_demographics
  11. where store_sales.ss_sold_date_sk = date_dim.d_date_sk
  12. and store_sales.ss_store_sk = store.s_store_sk
  13. and store_sales.ss_hdemo_sk = household_demographics.hd_demo_sk
  14. and date_dim.d_dom between 1 and 2
  15. and (household_demographics.hd_buy_potential = '1001-5000' or
  16. household_demographics.hd_buy_potential = '5001-10000')
  17. and household_demographics.hd_vehicle_count > 0
  18. and case when household_demographics.hd_vehicle_count > 0 then
  19. household_demographics.hd_dep_count/ household_demographics.hd_vehicle_count else null end > 1
  20. and date_dim.d_year in (1998,1998+1,1998+2)
  21. and ss_sold_date in (
  22. '1998-01-01', '1998-01-02', '1998-02-01', '1998-02-02', '1998-03-01', '1998-03-02',
  23. '1998-04-01', '1998-04-02', '1998-05-01', '1998-05-02', '1998-06-01', '1998-06-02',
  24. '1998-07-01', '1998-07-02', '1998-08-01', '1998-08-02', '1998-09-01', '1998-09-02',
  25. '1998-10-01', '1998-10-02', '1998-11-01', '1998-11-02', '1998-12-01', '1998-12-02',
  26. '1999-01-01', '1999-01-02', '1999-02-01', '1999-02-02', '1999-03-01', '1999-03-02',
  27. '1999-04-01', '1999-04-02', '1999-05-01', '1999-05-02', '1999-06-01', '1999-06-02',
  28. '1999-07-01', '1999-07-02', '1999-08-01', '1999-08-02', '1999-09-01', '1999-09-02',
  29. '1999-10-01', '1999-10-02', '1999-11-01', '1999-11-02', '1999-12-01', '1999-12-02',
  30. '2000-01-01', '2000-01-02', '2000-02-01', '2000-02-02', '2000-03-01', '2000-03-02',
  31. '2000-04-01', '2000-04-02', '2000-05-01', '2000-05-02', '2000-06-01', '2000-06-02',
  32. '2000-07-01', '2000-07-02', '2000-08-01', '2000-08-02', '2000-09-01', '2000-09-02',
  33. '2000-10-01', '2000-10-02', '2000-11-01', '2000-11-02', '2000-12-01', '2000-12-02'
  34. )
  35. and store.s_county in ('Kittitas County','Adams County','Richland County','Furnas County')
  36. group by ss_ticket_number,ss_customer_sk) dj,customer
  37. where dj.ss_customer_sk = customer.c_customer_sk
  38. and cnt between 1 and 5
  39. order by cnt desc;