query11.sql 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. -- start query 1 in stream 0 using template query11.tpl and seed 1819994127
  2. with year_total as (
  3. select c_customer_id customer_id
  4. ,c_first_name customer_first_name
  5. ,c_last_name customer_last_name
  6. ,c_preferred_cust_flag customer_preferred_cust_flag
  7. ,c_birth_country customer_birth_country
  8. ,c_login customer_login
  9. ,c_email_address customer_email_address
  10. ,d_year dyear
  11. ,sum(ss_ext_list_price-ss_ext_discount_amt) year_total
  12. ,'s' sale_type
  13. from customer
  14. ,store_sales
  15. ,date_dim
  16. where c_customer_sk = ss_customer_sk
  17. and ss_sold_date_sk = d_date_sk
  18. group by c_customer_id
  19. ,c_first_name
  20. ,c_last_name
  21. ,c_preferred_cust_flag
  22. ,c_birth_country
  23. ,c_login
  24. ,c_email_address
  25. ,d_year
  26. union all
  27. select c_customer_id customer_id
  28. ,c_first_name customer_first_name
  29. ,c_last_name customer_last_name
  30. ,c_preferred_cust_flag customer_preferred_cust_flag
  31. ,c_birth_country customer_birth_country
  32. ,c_login customer_login
  33. ,c_email_address customer_email_address
  34. ,d_year dyear
  35. ,sum(ws_ext_list_price-ws_ext_discount_amt) year_total
  36. ,'w' sale_type
  37. from customer
  38. ,web_sales
  39. ,date_dim
  40. where c_customer_sk = ws_bill_customer_sk
  41. and ws_sold_date_sk = d_date_sk
  42. group by c_customer_id
  43. ,c_first_name
  44. ,c_last_name
  45. ,c_preferred_cust_flag
  46. ,c_birth_country
  47. ,c_login
  48. ,c_email_address
  49. ,d_year
  50. )
  51. select
  52. t_s_secyear.customer_id
  53. ,t_s_secyear.customer_first_name
  54. ,t_s_secyear.customer_last_name
  55. ,t_s_secyear.customer_birth_country
  56. from year_total t_s_firstyear
  57. ,year_total t_s_secyear
  58. ,year_total t_w_firstyear
  59. ,year_total t_w_secyear
  60. where t_s_secyear.customer_id = t_s_firstyear.customer_id
  61. and t_s_firstyear.customer_id = t_w_secyear.customer_id
  62. and t_s_firstyear.customer_id = t_w_firstyear.customer_id
  63. and t_s_firstyear.sale_type = 's'
  64. and t_w_firstyear.sale_type = 'w'
  65. and t_s_secyear.sale_type = 's'
  66. and t_w_secyear.sale_type = 'w'
  67. and t_s_firstyear.dyear = 1999
  68. and t_s_secyear.dyear = 1999+1
  69. and t_w_firstyear.dyear = 1999
  70. and t_w_secyear.dyear = 1999+1
  71. and t_s_firstyear.year_total > 0
  72. and t_w_firstyear.year_total > 0
  73. and case when t_w_firstyear.year_total > 0 then t_w_secyear.year_total / t_w_firstyear.year_total else 0.0 end
  74. > case when t_s_firstyear.year_total > 0 then t_s_secyear.year_total / t_s_firstyear.year_total else 0.0 end
  75. order by t_s_secyear.customer_id
  76. ,t_s_secyear.customer_first_name
  77. ,t_s_secyear.customer_last_name
  78. ,t_s_secyear.customer_birth_country
  79. limit 100;
  80. -- end query 1 in stream 0 using template query11.tpl