|
| 1 | +{% include 'header.sql.jinja' %} |
| 2 | + |
| 3 | +-- NB: Subquerys |
| 4 | +$ss = |
| 5 | + (select store.s_store_sk s_store_sk, |
| 6 | + sum(ss_ext_sales_price) as sales, |
| 7 | + sum(ss_net_profit) as profit |
| 8 | + from {{store_sales}} as store_sales cross join |
| 9 | + {{date_dim}} as date_dim cross join |
| 10 | + {{store}} as store |
| 11 | + where ss_sold_date_sk = d_date_sk |
| 12 | + and cast(d_date as date) between cast('2000-08-16' as date) |
| 13 | + and (cast('2000-08-16' as date) + DateTime::IntervalFromDays(30)) |
| 14 | + and ss_store_sk = s_store_sk |
| 15 | + group by store.s_store_sk); |
| 16 | + |
| 17 | + $sr = |
| 18 | + (select store.s_store_sk s_store_sk, |
| 19 | + sum(sr_return_amt) as returns, |
| 20 | + sum(sr_net_loss) as profit_loss |
| 21 | + from {{store_returns}} as store_returns cross join |
| 22 | + {{date_dim}} as date_dim cross join |
| 23 | + {{store}} as store |
| 24 | + where sr_returned_date_sk = d_date_sk |
| 25 | + and cast(d_date as date) between cast('2000-08-16' as date) |
| 26 | + and (cast('2000-08-16' as date) + DateTime::IntervalFromDays(30)) |
| 27 | + and sr_store_sk = s_store_sk |
| 28 | + group by store.s_store_sk); |
| 29 | + $cs = |
| 30 | + (select catalog_sales.cs_call_center_sk cs_call_center_sk, |
| 31 | + sum(cs_ext_sales_price) as sales, |
| 32 | + sum(cs_net_profit) as profit |
| 33 | + from {{catalog_sales}} as catalog_sales cross join |
| 34 | + {{date_dim}} as date_dim |
| 35 | + where cs_sold_date_sk = d_date_sk |
| 36 | + and cast(d_date as date) between cast('2000-08-16' as date) |
| 37 | + and (cast('2000-08-16' as date) + DateTime::IntervalFromDays(30)) |
| 38 | + group by catalog_sales.cs_call_center_sk |
| 39 | + ); |
| 40 | + $cr = |
| 41 | + (select catalog_returns.cr_call_center_sk cr_call_center_sk, |
| 42 | + sum(cr_return_amount) as returns, |
| 43 | + sum(cr_net_loss) as profit_loss |
| 44 | + from {{catalog_returns}} as catalog_returns cross join |
| 45 | + {{date_dim}} as date_dim |
| 46 | + where cr_returned_date_sk = d_date_sk |
| 47 | + and cast(d_date as date) between cast('2000-08-16' as date) |
| 48 | + and (cast('2000-08-16' as date) + DateTime::IntervalFromDays(30)) |
| 49 | + group by catalog_returns.cr_call_center_sk |
| 50 | + ); |
| 51 | + $ws = |
| 52 | + ( select web_page.wp_web_page_sk wp_web_page_sk, |
| 53 | + sum(ws_ext_sales_price) as sales, |
| 54 | + sum(ws_net_profit) as profit |
| 55 | + from {{web_sales}} as web_sales cross join |
| 56 | + {{date_dim}} as date_dim cross join |
| 57 | + {{web_page}} as web_page |
| 58 | + where ws_sold_date_sk = d_date_sk |
| 59 | + and cast(d_date as date) between cast('2000-08-16' as date) |
| 60 | + and (cast('2000-08-16' as date) + DateTime::IntervalFromDays(30)) |
| 61 | + and ws_web_page_sk = wp_web_page_sk |
| 62 | + group by web_page.wp_web_page_sk); |
| 63 | + $wr = |
| 64 | + (select web_page.wp_web_page_sk wp_web_page_sk, |
| 65 | + sum(wr_return_amt) as returns, |
| 66 | + sum(wr_net_loss) as profit_loss |
| 67 | + from {{web_returns}} as web_returns cross join |
| 68 | + {{date_dim}} as date_dim cross join |
| 69 | + {{web_page}} as web_page |
| 70 | + where wr_returned_date_sk = d_date_sk |
| 71 | + and cast(d_date as date) between cast('2000-08-16' as date) |
| 72 | + and (cast('2000-08-16' as date) + DateTime::IntervalFromDays(30)) |
| 73 | + and wr_web_page_sk = wp_web_page_sk |
| 74 | + group by web_page.wp_web_page_sk); |
| 75 | +-- start query 1 in stream 0 using template query77.tpl and seed 1819994127 |
| 76 | + select channel |
| 77 | + , id |
| 78 | + , sum(sales) as sales |
| 79 | + , sum(returns) as returns |
| 80 | + , sum(profit) as profit |
| 81 | + from |
| 82 | + (select 'store channel' as channel |
| 83 | + , ss.s_store_sk as id |
| 84 | + , sales |
| 85 | + , coalesce(returns, 0) as returns |
| 86 | + , (profit - coalesce(profit_loss,0)) as profit |
| 87 | + from $ss ss left join $sr sr |
| 88 | + on ss.s_store_sk = sr.s_store_sk |
| 89 | + union all |
| 90 | + select 'catalog channel' as channel |
| 91 | + , cs_call_center_sk as id |
| 92 | + , sales |
| 93 | + , returns |
| 94 | + , (profit - profit_loss) as profit |
| 95 | + from $cs cs |
| 96 | + cross join $cr cr |
| 97 | + union all |
| 98 | + select 'web channel' as channel |
| 99 | + , ws.wp_web_page_sk as id |
| 100 | + , sales |
| 101 | + , coalesce(returns, 0) returns |
| 102 | + , (profit - coalesce(profit_loss,0)) as profit |
| 103 | + from $ws ws left join $wr wr |
| 104 | + on ws.wp_web_page_sk = wr.wp_web_page_sk |
| 105 | + ) x |
| 106 | + group by rollup (channel, id) |
| 107 | + order by channel |
| 108 | + ,id |
| 109 | + limit 100; |
| 110 | + |
| 111 | +-- end query 1 in stream 0 using template query77.tpl |
0 commit comments