|
| 1 | +--q14a.sql-- |
| 2 | + |
| 3 | +with cross_items as |
| 4 | + (select i_item_sk ss_item_sk |
| 5 | + from item, |
| 6 | + (select iss.i_brand_id brand_id, iss.i_class_id class_id, iss.i_category_id category_id |
| 7 | + from store_sales, item iss, date_dim d1 |
| 8 | + where ss_item_sk = iss.i_item_sk |
| 9 | + and ss_sold_date_sk = d1.d_date_sk |
| 10 | + and d1.d_year between 1999 AND 1999 + 2 |
| 11 | + intersect |
| 12 | + select ics.i_brand_id, ics.i_class_id, ics.i_category_id |
| 13 | + from catalog_sales, item ics, date_dim d2 |
| 14 | + where cs_item_sk = ics.i_item_sk |
| 15 | + and cs_sold_date_sk = d2.d_date_sk |
| 16 | + and d2.d_year between 1999 AND 1999 + 2 |
| 17 | + intersect |
| 18 | + select iws.i_brand_id, iws.i_class_id, iws.i_category_id |
| 19 | + from web_sales, item iws, date_dim d3 |
| 20 | + where ws_item_sk = iws.i_item_sk |
| 21 | + and ws_sold_date_sk = d3.d_date_sk |
| 22 | + and d3.d_year between 1999 AND 1999 + 2) x |
| 23 | + where i_brand_id = brand_id |
| 24 | + and i_class_id = class_id |
| 25 | + and i_category_id = category_id |
| 26 | +), |
| 27 | + avg_sales as |
| 28 | + (select avg(quantity*list_price) average_sales |
| 29 | + from ( |
| 30 | + select ss_quantity quantity, ss_list_price list_price |
| 31 | + from store_sales, date_dim |
| 32 | + where ss_sold_date_sk = d_date_sk |
| 33 | + and d_year between 1999 and 2001 |
| 34 | + union all |
| 35 | + select cs_quantity quantity, cs_list_price list_price |
| 36 | + from catalog_sales, date_dim |
| 37 | + where cs_sold_date_sk = d_date_sk |
| 38 | + and d_year between 1999 and 1999 + 2 |
| 39 | + union all |
| 40 | + select ws_quantity quantity, ws_list_price list_price |
| 41 | + from web_sales, date_dim |
| 42 | + where ws_sold_date_sk = d_date_sk |
| 43 | + and d_year between 1999 and 1999 + 2) x) |
| 44 | + select channel, i_brand_id,i_class_id,i_category_id,sum(sales), sum(number_sales) |
| 45 | + from( |
| 46 | + select 'store' channel, i_brand_id,i_class_id |
| 47 | + ,i_category_id,sum(ss_quantity*ss_list_price) sales |
| 48 | + , count(*) number_sales |
| 49 | + from store_sales, item, date_dim |
| 50 | + where ss_item_sk in (select ss_item_sk from cross_items) |
| 51 | + and ss_item_sk = i_item_sk |
| 52 | + and ss_sold_date_sk = d_date_sk |
| 53 | + and d_year = 1999+2 |
| 54 | + and d_moy = 11 |
| 55 | + group by i_brand_id,i_class_id,i_category_id |
| 56 | + having sum(ss_quantity*ss_list_price) > (select average_sales from avg_sales) |
| 57 | + union all |
| 58 | + select 'catalog' channel, i_brand_id,i_class_id,i_category_id, sum(cs_quantity*cs_list_price) sales, count(*) number_sales |
| 59 | + from catalog_sales, item, date_dim |
| 60 | + where cs_item_sk in (select ss_item_sk from cross_items) |
| 61 | + and cs_item_sk = i_item_sk |
| 62 | + and cs_sold_date_sk = d_date_sk |
| 63 | + and d_year = 1999+2 |
| 64 | + and d_moy = 11 |
| 65 | + group by i_brand_id,i_class_id,i_category_id |
| 66 | + having sum(cs_quantity*cs_list_price) > (select average_sales from avg_sales) |
| 67 | + union all |
| 68 | + select 'web' channel, i_brand_id,i_class_id,i_category_id, sum(ws_quantity*ws_list_price) sales , count(*) number_sales |
| 69 | + from web_sales, item, date_dim |
| 70 | + where ws_item_sk in (select ss_item_sk from cross_items) |
| 71 | + and ws_item_sk = i_item_sk |
| 72 | + and ws_sold_date_sk = d_date_sk |
| 73 | + and d_year = 1999+2 |
| 74 | + and d_moy = 11 |
| 75 | + group by i_brand_id,i_class_id,i_category_id |
| 76 | + having sum(ws_quantity*ws_list_price) > (select average_sales from avg_sales) |
| 77 | + ) y |
| 78 | + group by rollup (channel, i_brand_id,i_class_id,i_category_id) |
| 79 | + order by channel,i_brand_id,i_class_id,i_category_id |
| 80 | + limit 100 |
| 81 | + |
0 commit comments