query54.sql 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. with my_customers as (
  2. select distinct c_customer_sk
  3. , c_current_addr_sk
  4. from
  5. ( select cs_sold_date_sk sold_date_sk,
  6. cs_bill_customer_sk customer_sk,
  7. cs_item_sk item_sk
  8. from catalog_sales
  9. union all
  10. select ws_sold_date_sk sold_date_sk,
  11. ws_bill_customer_sk customer_sk,
  12. ws_item_sk item_sk
  13. from web_sales
  14. ) cs_or_ws_sales,
  15. item,
  16. date_dim,
  17. customer
  18. where sold_date_sk = d_date_sk
  19. and item_sk = i_item_sk
  20. and i_category = 'Jewelry'
  21. and i_class = 'football'
  22. and c_customer_sk = cs_or_ws_sales.customer_sk
  23. and d_moy = 3
  24. and d_year = 2000
  25. )
  26. , my_revenue as (
  27. select c_customer_sk,
  28. sum(ss_ext_sales_price) as revenue
  29. from my_customers,
  30. store_sales,
  31. customer_address,
  32. store,
  33. date_dim
  34. where c_current_addr_sk = ca_address_sk
  35. and ca_county = s_county
  36. and ca_state = s_state
  37. and ss_sold_date_sk = d_date_sk
  38. and c_customer_sk = ss_customer_sk
  39. and d_month_seq between (select distinct d_month_seq+1
  40. from date_dim where d_year = 2000 and d_moy = 3)
  41. and (select distinct d_month_seq+3
  42. from date_dim where d_year = 2000 and d_moy = 3)
  43. group by c_customer_sk
  44. )
  45. , segments as
  46. (select cast((revenue/50) as int) as segment
  47. from my_revenue
  48. )
  49. select segment, count(*) as num_customers, segment*50 as segment_base
  50. from segments
  51. group by segment
  52. order by segment, num_customers
  53. limit 100;