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