|
| 1 | +with cross_items as |
| 2 | +(select i_item_sk ss_item_sk |
| 3 | +from item, |
| 4 | +(select iss.i_brand_id brand_id |
| 5 | +,iss.i_class_id class_id |
| 6 | +,iss.i_category_id category_id |
| 7 | +from store_sales |
| 8 | +,item iss |
| 9 | +,date_dim d1 |
| 10 | +where ss_item_sk = iss.i_item_sk |
| 11 | +and ss_sold_date_sk = d1.d_date_sk |
| 12 | +and d1.d_year between 1999 AND 1999 + 2 |
| 13 | +intersect |
| 14 | +select ics.i_brand_id |
| 15 | +,ics.i_class_id |
| 16 | +,ics.i_category_id |
| 17 | +from catalog_sales |
| 18 | +,item ics |
| 19 | +,date_dim d2 |
| 20 | +where cs_item_sk = ics.i_item_sk |
| 21 | +and cs_sold_date_sk = d2.d_date_sk |
| 22 | +and d2.d_year between 1999 AND 1999 + 2 |
| 23 | +intersect |
| 24 | +select iws.i_brand_id |
| 25 | +,iws.i_class_id |
| 26 | +,iws.i_category_id |
| 27 | +from web_sales |
| 28 | +,item iws |
| 29 | +,date_dim d3 |
| 30 | +where ws_item_sk = iws.i_item_sk |
| 31 | +and ws_sold_date_sk = d3.d_date_sk |
| 32 | +and d3.d_year between 1999 AND 1999 + 2) x |
| 33 | +where i_brand_id = brand_id |
| 34 | +and i_class_id = class_id |
| 35 | +and i_category_id = category_id |
| 36 | +), |
| 37 | +avg_sales as |
| 38 | +(select avg(quantitylist_price) average_sales |
| 39 | +from (select ss_quantity quantity |
| 40 | +,ss_list_price list_price |
| 41 | +from store_sales |
| 42 | +,date_dim |
| 43 | +where ss_sold_date_sk = d_date_sk |
| 44 | +and d_year between 1999 and 2001 |
| 45 | +union all |
| 46 | +select cs_quantity quantity |
| 47 | +,cs_list_price list_price |
| 48 | +from catalog_sales |
| 49 | +,date_dim |
| 50 | +where cs_sold_date_sk = d_date_sk |
| 51 | +and d_year between 1998 and 1998 + 2 |
| 52 | +union all |
| 53 | +select ws_quantity quantity |
| 54 | +,ws_list_price list_price |
| 55 | +from web_sales |
| 56 | +,date_dim |
| 57 | +where ws_sold_date_sk = d_date_sk |
| 58 | +and d_year between 1998 and 1998 + 2) x) |
| 59 | +select channel, i_brand_id,i_class_id,i_category_id,sum(sales), sum(number_sales) |
| 60 | +from( |
| 61 | +select 'store' channel, i_brand_id,i_class_id |
| 62 | +,i_category_id,sum(ss_quantityss_list_price) sales |
| 63 | +, count() number_sales |
| 64 | +from store_sales |
| 65 | +,item |
| 66 | +,date_dim |
| 67 | +where ss_item_sk in (select ss_item_sk from cross_items) |
| 68 | +and ss_item_sk = i_item_sk |
| 69 | +and ss_sold_date_sk = d_date_sk |
| 70 | +and d_year = 1998+2 |
| 71 | +and d_moy = 11 |
| 72 | +group by i_brand_id,i_class_id,i_category_id |
| 73 | +having sum(ss_quantityss_list_price) > (select average_sales from avg_sales) |
| 74 | +union all |
| 75 | +select 'catalog' channel, i_brand_id,i_class_id,i_category_id, sum(cs_quantitycs_list_price) sales, count() number_sales |
| 76 | +from catalog_sales |
| 77 | +,item |
| 78 | +,date_dim |
| 79 | +where cs_item_sk in (select ss_item_sk from cross_items) |
| 80 | +and cs_item_sk = i_item_sk |
| 81 | +and cs_sold_date_sk = d_date_sk |
| 82 | +and d_year = 1998+2 |
| 83 | +and d_moy = 11 |
| 84 | +group by i_brand_id,i_class_id,i_category_id |
| 85 | +having sum(cs_quantitycs_list_price) > (select average_sales from avg_sales) |
| 86 | +union all |
| 87 | +select 'web' channel, i_brand_id,i_class_id,i_category_id, sum(ws_quantityws_list_price) sales , count() number_sales |
| 88 | +from web_sales |
| 89 | +,item |
| 90 | +,date_dim |
| 91 | +where ws_item_sk in (select ss_item_sk from cross_items) |
| 92 | +and ws_item_sk = i_item_sk |
| 93 | +and ws_sold_date_sk = d_date_sk |
| 94 | +and d_year = 1998+2 |
| 95 | +and d_moy = 11 |
| 96 | +group by i_brand_id,i_class_id,i_category_id |
| 97 | +having sum(ws_quantityws_list_price) > (select average_sales from avg_sales) |
| 98 | +) y |
| 99 | +group by rollup (channel, i_brand_id,i_class_id,i_category_id) |
| 100 | +order by channel,i_brand_id,i_class_id,i_category_id |
| 101 | +limit 100; |
| 102 | +with cross_items as |
| 103 | +(select i_item_sk ss_item_sk |
| 104 | +from item, |
| 105 | +(select iss.i_brand_id brand_id |
| 106 | +,iss.i_class_id class_id |
| 107 | +,iss.i_category_id category_id |
| 108 | +from store_sales |
| 109 | +,item iss |
| 110 | +,date_dim d1 |
| 111 | +where ss_item_sk = iss.i_item_sk |
| 112 | +and ss_sold_date_sk = d1.d_date_sk |
| 113 | +and d1.d_year between 1999 AND 1999 + 2 |
| 114 | +intersect |
| 115 | +select ics.i_brand_id |
| 116 | +,ics.i_class_id |
| 117 | +,ics.i_category_id |
| 118 | +from catalog_sales |
| 119 | +,item ics |
| 120 | +,date_dim d2 |
| 121 | +where cs_item_sk = ics.i_item_sk |
| 122 | +and cs_sold_date_sk = d2.d_date_sk |
| 123 | +and d2.d_year between 1999 AND 1999 + 2 |
| 124 | +intersect |
| 125 | +select iws.i_brand_id |
| 126 | +,iws.i_class_id |
| 127 | +,iws.i_category_id |
| 128 | +from web_sales |
| 129 | +,item iws |
| 130 | +,date_dim d3 |
| 131 | +where ws_item_sk = iws.i_item_sk |
| 132 | +and ws_sold_date_sk = d3.d_date_sk |
| 133 | +and d3.d_year between 1999 AND 1999 + 2) x |
| 134 | +where i_brand_id = brand_id |
| 135 | +and i_class_id = class_id |
| 136 | +and i_category_id = category_id |
| 137 | +), |
| 138 | +avg_sales as |
| 139 | +(select avg(quantitylist_price) average_sales |
| 140 | +from (select ss_quantity quantity |
| 141 | +,ss_list_price list_price |
| 142 | +from store_sales |
| 143 | +,date_dim |
| 144 | +where ss_sold_date_sk = d_date_sk |
| 145 | +and d_year between 1998 and 1998 + 2 |
| 146 | +union all |
| 147 | +select cs_quantity quantity |
| 148 | +,cs_list_price list_price |
| 149 | +from catalog_sales |
| 150 | +,date_dim |
| 151 | +where cs_sold_date_sk = d_date_sk |
| 152 | +and d_year between 1998 and 1998 + 2 |
| 153 | +union all |
| 154 | +select ws_quantity quantity |
| 155 | +,ws_list_price list_price |
| 156 | +from web_sales |
| 157 | +,date_dim |
| 158 | +where ws_sold_date_sk = d_date_sk |
| 159 | +and d_year between 1998 and 1998 + 2) x) |
| 160 | +select * from |
| 161 | +(select 'store' channel, i_brand_id,i_class_id,i_category_id |
| 162 | +,sum(ss_quantityss_list_price) sales, count() number_sales |
| 163 | +from store_sales |
| 164 | +,item |
| 165 | +,date_dim |
| 166 | +where ss_item_sk in (select ss_item_sk from cross_items) |
| 167 | +and ss_item_sk = i_item_sk |
| 168 | +and ss_sold_date_sk = d_date_sk |
| 169 | +and d_week_seq = (select d_week_seq |
| 170 | +from date_dim |
| 171 | +where d_year = 1998 + 1 |
| 172 | +and d_moy = 12 |
| 173 | +and d_dom = 16) |
| 174 | +group by i_brand_id,i_class_id,i_category_id |
| 175 | +having sum(ss_quantityss_list_price) > (select average_sales from avg_sales)) this_year, |
| 176 | +(select 'store' channel, i_brand_id,i_class_id |
| 177 | +,i_category_id, sum(ss_quantityss_list_price) sales, count() number_sales |
| 178 | +from store_sales |
| 179 | +,item |
| 180 | +,date_dim |
| 181 | +where ss_item_sk in (select ss_item_sk from cross_items) |
| 182 | +and ss_item_sk = i_item_sk |
| 183 | +and ss_sold_date_sk = d_date_sk |
| 184 | +and d_week_seq = (select d_week_seq |
| 185 | +from date_dim |
| 186 | +where d_year = 1998 |
| 187 | +and d_moy = 12 |
| 188 | +and d_dom = 16) |
| 189 | +group by i_brand_id,i_class_id,i_category_id |
| 190 | +having sum(ss_quantity*ss_list_price) > (select average_sales from avg_sales)) last_year |
| 191 | +where this_year.i_brand_id= last_year.i_brand_id |
| 192 | +and this_year.i_class_id = last_year.i_class_id |
| 193 | +and this_year.i_category_id = last_year.i_category_id |
| 194 | +order by this_year.channel, this_year.i_brand_id, this_year.i_class_id, this_year.i_category_id |
| 195 | +limit 100; |
0 commit comments