query34.sql 1.3 KB

12345678910111213141516171819202122232425262728
  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 3 or date_dim.d_dom between 25 and 28)
  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
  19. then household_demographics.hd_dep_count/ household_demographics.hd_vehicle_count
  20. else null
  21. end) > 1.2
  22. and date_dim.d_year in (1998,1998+1,1998+2)
  23. and store.s_county in ('Kittitas County','Adams County','Richland County','Furnas County',
  24. 'Orange County','Appanoose County','Franklin Parish','Tehama County')
  25. group by ss_ticket_number,ss_customer_sk) dn,customer
  26. where dn.ss_customer_sk = customer.c_customer_sk
  27. and cnt between 15 and 20
  28. order by c_last_name,c_first_name,c_salutation,c_preferred_cust_flag desc;