q74.sql 2.2 KB

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