1
0

query11.sql 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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
  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. ,d_year
  22. ,c_preferred_cust_flag
  23. ,c_birth_country
  24. ,c_login
  25. ,c_email_address
  26. ,d_year
  27. union all
  28. select c_customer_id customer_id
  29. ,c_first_name customer_first_name
  30. ,c_last_name customer_last_name
  31. ,c_preferred_cust_flag
  32. ,c_birth_country customer_birth_country
  33. ,c_login customer_login
  34. ,c_email_address customer_email_address
  35. ,d_year dyear
  36. ,sum(ws_ext_list_price-ws_ext_discount_amt) year_total
  37. ,'w' sale_type
  38. from customer
  39. ,web_sales
  40. ,date_dim
  41. where c_customer_sk = ws_bill_customer_sk
  42. and ws_sold_date_sk = d_date_sk
  43. group by c_customer_id
  44. ,c_first_name
  45. ,c_last_name
  46. ,c_preferred_cust_flag
  47. ,c_birth_country
  48. ,c_login
  49. ,c_email_address
  50. ,d_year
  51. )
  52. select t_s_secyear.c_preferred_cust_flag
  53. from year_total t_s_firstyear
  54. ,year_total t_s_secyear
  55. ,year_total t_w_firstyear
  56. ,year_total t_w_secyear
  57. where t_s_secyear.customer_id = t_s_firstyear.customer_id
  58. and t_s_firstyear.customer_id = t_w_secyear.customer_id
  59. and t_s_firstyear.customer_id = t_w_firstyear.customer_id
  60. and t_s_firstyear.sale_type = 's'
  61. and t_w_firstyear.sale_type = 'w'
  62. and t_s_secyear.sale_type = 's'
  63. and t_w_secyear.sale_type = 'w'
  64. and t_s_firstyear.dyear = 2001
  65. and t_s_secyear.dyear = 2001+1
  66. and t_w_firstyear.dyear = 2001
  67. and t_w_secyear.dyear = 2001+1
  68. and t_s_firstyear.year_total > 0
  69. and t_w_firstyear.year_total > 0
  70. and case when t_w_firstyear.year_total > 0 then t_w_secyear.year_total / t_w_firstyear.year_total else null end
  71. > case when t_s_firstyear.year_total > 0 then t_s_secyear.year_total / t_s_firstyear.year_total else null end
  72. order by t_s_secyear.c_preferred_cust_flag
  73. limit 100;
  74. -- end query 1 in stream 0 using template query11.tpl