q04.sql 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. --!syntax_pg
  2. --TPC-DS Q4
  3. -- start query 1 in stream 0 using template ../query_templates/query4.tpl
  4. with year_total as (
  5. select c_customer_id customer_id
  6. ,c_first_name customer_first_name
  7. ,c_last_name customer_last_name
  8. ,c_preferred_cust_flag customer_preferred_cust_flag
  9. ,c_birth_country customer_birth_country
  10. ,c_login customer_login
  11. ,c_email_address customer_email_address
  12. ,d_year dyear
  13. ,sum(((ss_ext_list_price-ss_ext_wholesale_cost-ss_ext_discount_amt)+ss_ext_sales_price)/2::numeric) year_total
  14. ,'s' sale_type
  15. from plato.customer
  16. ,plato.store_sales
  17. ,plato.date_dim
  18. where c_customer_sk = ss_customer_sk
  19. and ss_sold_date_sk = d_date_sk
  20. group by c_customer_id
  21. ,c_first_name
  22. ,c_last_name
  23. ,c_preferred_cust_flag
  24. ,c_birth_country
  25. ,c_login
  26. ,c_email_address
  27. ,d_year
  28. union all
  29. select c_customer_id customer_id
  30. ,c_first_name customer_first_name
  31. ,c_last_name customer_last_name
  32. ,c_preferred_cust_flag customer_preferred_cust_flag
  33. ,c_birth_country customer_birth_country
  34. ,c_login customer_login
  35. ,c_email_address customer_email_address
  36. ,d_year dyear
  37. ,sum((((cs_ext_list_price-cs_ext_wholesale_cost-cs_ext_discount_amt)+cs_ext_sales_price)/2::numeric) ) year_total
  38. ,'c' sale_type
  39. from plato.customer
  40. ,plato.catalog_sales
  41. ,plato.date_dim
  42. where c_customer_sk = cs_bill_customer_sk
  43. and cs_sold_date_sk = d_date_sk
  44. group by c_customer_id
  45. ,c_first_name
  46. ,c_last_name
  47. ,c_preferred_cust_flag
  48. ,c_birth_country
  49. ,c_login
  50. ,c_email_address
  51. ,d_year
  52. union all
  53. select c_customer_id customer_id
  54. ,c_first_name customer_first_name
  55. ,c_last_name customer_last_name
  56. ,c_preferred_cust_flag customer_preferred_cust_flag
  57. ,c_birth_country customer_birth_country
  58. ,c_login customer_login
  59. ,c_email_address customer_email_address
  60. ,d_year dyear
  61. ,sum((((ws_ext_list_price-ws_ext_wholesale_cost-ws_ext_discount_amt)+ws_ext_sales_price)/2::numeric) ) year_total
  62. ,'w' sale_type
  63. from plato.customer
  64. ,plato.web_sales
  65. ,plato.date_dim
  66. where c_customer_sk = ws_bill_customer_sk
  67. and ws_sold_date_sk = d_date_sk
  68. group by c_customer_id
  69. ,c_first_name
  70. ,c_last_name
  71. ,c_preferred_cust_flag
  72. ,c_birth_country
  73. ,c_login
  74. ,c_email_address
  75. ,d_year
  76. )
  77. select
  78. t_s_secyear.customer_id
  79. ,t_s_secyear.customer_first_name
  80. ,t_s_secyear.customer_last_name
  81. ,t_s_secyear.customer_email_address
  82. from year_total t_s_firstyear
  83. ,year_total t_s_secyear
  84. ,year_total t_c_firstyear
  85. ,year_total t_c_secyear
  86. ,year_total t_w_firstyear
  87. ,year_total t_w_secyear
  88. where t_s_secyear.customer_id = t_s_firstyear.customer_id
  89. and t_s_firstyear.customer_id = t_c_secyear.customer_id
  90. and t_s_firstyear.customer_id = t_c_firstyear.customer_id
  91. and t_s_firstyear.customer_id = t_w_firstyear.customer_id
  92. and t_s_firstyear.customer_id = t_w_secyear.customer_id
  93. and t_s_firstyear.sale_type = 's'
  94. and t_c_firstyear.sale_type = 'c'
  95. and t_w_firstyear.sale_type = 'w'
  96. and t_s_secyear.sale_type = 's'
  97. and t_c_secyear.sale_type = 'c'
  98. and t_w_secyear.sale_type = 'w'
  99. and t_s_firstyear.dyear = 2001
  100. and t_s_secyear.dyear = 2001+1
  101. and t_c_firstyear.dyear = 2001
  102. and t_c_secyear.dyear = 2001+1
  103. and t_w_firstyear.dyear = 2001
  104. and t_w_secyear.dyear = 2001+1
  105. and t_s_firstyear.year_total > 0::numeric
  106. and t_c_firstyear.year_total > 0::numeric
  107. and t_w_firstyear.year_total > 0::numeric
  108. and case when t_c_firstyear.year_total > 0::numeric then t_c_secyear.year_total / t_c_firstyear.year_total else null::numeric end
  109. > case when t_s_firstyear.year_total > 0::numeric then t_s_secyear.year_total / t_s_firstyear.year_total else null::numeric end
  110. and case when t_c_firstyear.year_total > 0::numeric then t_c_secyear.year_total / t_c_firstyear.year_total else null::numeric end
  111. > case when t_w_firstyear.year_total > 0::numeric then t_w_secyear.year_total / t_w_firstyear.year_total else null::numeric end
  112. order by t_s_secyear.customer_id
  113. ,t_s_secyear.customer_first_name
  114. ,t_s_secyear.customer_last_name
  115. ,t_s_secyear.customer_email_address
  116. limit 100;
  117. -- end query 1 in stream 0 using template ../query_templates/query4.tpl