q54.sql 1.9 KB

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