query74.sql 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. -- start query 1 in stream 0 using template query74.tpl and seed 1556717815
  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. ,d_year as year
  7. ,sum(ss_net_paid) year_total
  8. ,'s' sale_type
  9. from customer
  10. ,store_sales
  11. ,date_dim
  12. where c_customer_sk = ss_customer_sk
  13. and ss_sold_date_sk = d_date_sk
  14. and d_year in (1998,1998+1)
  15. group by c_customer_id
  16. ,c_first_name
  17. ,c_last_name
  18. ,d_year
  19. union all
  20. select c_customer_id customer_id
  21. ,c_first_name customer_first_name
  22. ,c_last_name customer_last_name
  23. ,d_year as year
  24. ,sum(ws_net_paid) year_total
  25. ,'w' sale_type
  26. from customer
  27. ,web_sales
  28. ,date_dim
  29. where c_customer_sk = ws_bill_customer_sk
  30. and ws_sold_date_sk = d_date_sk
  31. and d_year in (1998,1998+1)
  32. group by c_customer_id
  33. ,c_first_name
  34. ,c_last_name
  35. ,d_year
  36. )
  37. select
  38. t_s_secyear.customer_id, t_s_secyear.customer_first_name, t_s_secyear.customer_last_name
  39. from year_total t_s_firstyear
  40. ,year_total t_s_secyear
  41. ,year_total t_w_firstyear
  42. ,year_total t_w_secyear
  43. where t_s_secyear.customer_id = t_s_firstyear.customer_id
  44. and t_s_firstyear.customer_id = t_w_secyear.customer_id
  45. and t_s_firstyear.customer_id = t_w_firstyear.customer_id
  46. and t_s_firstyear.sale_type = 's'
  47. and t_w_firstyear.sale_type = 'w'
  48. and t_s_secyear.sale_type = 's'
  49. and t_w_secyear.sale_type = 'w'
  50. and t_s_firstyear.year = 1998
  51. and t_s_secyear.year = 1998+1
  52. and t_w_firstyear.year = 1998
  53. and t_w_secyear.year = 1998+1
  54. and t_s_firstyear.year_total > 0
  55. and t_w_firstyear.year_total > 0
  56. and case when t_w_firstyear.year_total > 0 then t_w_secyear.year_total / t_w_firstyear.year_total else null end
  57. > case when t_s_firstyear.year_total > 0 then t_s_secyear.year_total / t_s_firstyear.year_total else null end
  58. order by 3,1,2
  59. limit 100;
  60. -- end query 1 in stream 0 using template query74.tpl