q11.sql 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. --!syntax_pg
  2. --TPC-DS Q11
  3. -- start query 1 in stream 0 using template ../query_templates/query11.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_discount_amt) 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(ws_ext_list_price-ws_ext_discount_amt) year_total
  38. ,'w' sale_type
  39. from plato.customer
  40. ,plato.web_sales
  41. ,plato.date_dim
  42. where c_customer_sk = ws_bill_customer_sk
  43. and ws_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. )
  53. select
  54. t_s_secyear.customer_id
  55. ,t_s_secyear.customer_first_name
  56. ,t_s_secyear.customer_last_name
  57. ,t_s_secyear.customer_email_address
  58. from year_total t_s_firstyear
  59. ,year_total t_s_secyear
  60. ,year_total t_w_firstyear
  61. ,year_total t_w_secyear
  62. where t_s_secyear.customer_id = t_s_firstyear.customer_id
  63. and t_s_firstyear.customer_id = t_w_secyear.customer_id
  64. and t_s_firstyear.customer_id = t_w_firstyear.customer_id
  65. and t_s_firstyear.sale_type = 's'
  66. and t_w_firstyear.sale_type = 'w'
  67. and t_s_secyear.sale_type = 's'
  68. and t_w_secyear.sale_type = 'w'
  69. and t_s_firstyear.dyear = 2001
  70. and t_s_secyear.dyear = 2001+1
  71. and t_w_firstyear.dyear = 2001
  72. and t_w_secyear.dyear = 2001+1
  73. and t_s_firstyear.year_total > 0::numeric
  74. and t_w_firstyear.year_total > 0::numeric
  75. and case when t_w_firstyear.year_total > 0::numeric then t_w_secyear.year_total / t_w_firstyear.year_total else 0.0::numeric end
  76. > case when t_s_firstyear.year_total > 0::numeric then t_s_secyear.year_total / t_s_firstyear.year_total else 0.0::numeric end
  77. order by t_s_secyear.customer_id
  78. ,t_s_secyear.customer_first_name
  79. ,t_s_secyear.customer_last_name
  80. ,t_s_secyear.customer_email_address
  81. limit 100;
  82. -- end query 1 in stream 0 using template ../query_templates/query11.tpl