query54.sql 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. with my_customers as (
  2. select 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. group by c_customer_sk
  26. , c_current_addr_sk
  27. )
  28. , my_revenue as (
  29. select c_customer_sk,
  30. sum(ss_ext_sales_price) as revenue
  31. from my_customers,
  32. store_sales,
  33. customer_address,
  34. store,
  35. date_dim
  36. where c_current_addr_sk = ca_address_sk
  37. and ca_county = s_county
  38. and ca_state = s_state
  39. and ss_sold_date_sk = d_date_sk
  40. and c_customer_sk = ss_customer_sk
  41. and d_month_seq between (1203)
  42. and (1205)
  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;