forked from agirish/tpcds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query14.sql
245 lines (244 loc) · 10.1 KB
/
query14.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
-- start query 14 in stream 0 using template query14.tpl
WITH cross_items
AS (SELECT i_item_sk ss_item_sk
FROM item,
(SELECT iss.i_brand_id brand_id,
iss.i_class_id class_id,
iss.i_category_id category_id
FROM store_sales,
item iss,
date_dim d1
WHERE ss_item_sk = iss.i_item_sk
AND ss_sold_date_sk = d1.d_date_sk
AND d1.d_year BETWEEN 1999 AND 1999 + 2
INTERSECT
SELECT ics.i_brand_id,
ics.i_class_id,
ics.i_category_id
FROM catalog_sales,
item ics,
date_dim d2
WHERE cs_item_sk = ics.i_item_sk
AND cs_sold_date_sk = d2.d_date_sk
AND d2.d_year BETWEEN 1999 AND 1999 + 2
INTERSECT
SELECT iws.i_brand_id,
iws.i_class_id,
iws.i_category_id
FROM web_sales,
item iws,
date_dim d3
WHERE ws_item_sk = iws.i_item_sk
AND ws_sold_date_sk = d3.d_date_sk
AND d3.d_year BETWEEN 1999 AND 1999 + 2)
WHERE i_brand_id = brand_id
AND i_class_id = class_id
AND i_category_id = category_id),
avg_sales
AS (SELECT Avg(quantity * list_price) average_sales
FROM (SELECT ss_quantity quantity,
ss_list_price list_price
FROM store_sales,
date_dim
WHERE ss_sold_date_sk = d_date_sk
AND d_year BETWEEN 1999 AND 1999 + 2
UNION ALL
SELECT cs_quantity quantity,
cs_list_price list_price
FROM catalog_sales,
date_dim
WHERE cs_sold_date_sk = d_date_sk
AND d_year BETWEEN 1999 AND 1999 + 2
UNION ALL
SELECT ws_quantity quantity,
ws_list_price list_price
FROM web_sales,
date_dim
WHERE ws_sold_date_sk = d_date_sk
AND d_year BETWEEN 1999 AND 1999 + 2) x)
SELECT channel,
i_brand_id,
i_class_id,
i_category_id,
Sum(sales),
Sum(number_sales)
FROM (SELECT 'store' channel,
i_brand_id,
i_class_id,
i_category_id,
Sum(ss_quantity * ss_list_price) sales,
Count(*) number_sales
FROM store_sales,
item,
date_dim
WHERE ss_item_sk IN (SELECT ss_item_sk
FROM cross_items)
AND ss_item_sk = i_item_sk
AND ss_sold_date_sk = d_date_sk
AND d_year = 1999 + 2
AND d_moy = 11
GROUP BY i_brand_id,
i_class_id,
i_category_id
HAVING Sum(ss_quantity * ss_list_price) > (SELECT average_sales
FROM avg_sales)
UNION ALL
SELECT 'catalog' channel,
i_brand_id,
i_class_id,
i_category_id,
Sum(cs_quantity * cs_list_price) sales,
Count(*) number_sales
FROM catalog_sales,
item,
date_dim
WHERE cs_item_sk IN (SELECT ss_item_sk
FROM cross_items)
AND cs_item_sk = i_item_sk
AND cs_sold_date_sk = d_date_sk
AND d_year = 1999 + 2
AND d_moy = 11
GROUP BY i_brand_id,
i_class_id,
i_category_id
HAVING Sum(cs_quantity * cs_list_price) > (SELECT average_sales
FROM avg_sales)
UNION ALL
SELECT 'web' channel,
i_brand_id,
i_class_id,
i_category_id,
Sum(ws_quantity * ws_list_price) sales,
Count(*) number_sales
FROM web_sales,
item,
date_dim
WHERE ws_item_sk IN (SELECT ss_item_sk
FROM cross_items)
AND ws_item_sk = i_item_sk
AND ws_sold_date_sk = d_date_sk
AND d_year = 1999 + 2
AND d_moy = 11
GROUP BY i_brand_id,
i_class_id,
i_category_id
HAVING Sum(ws_quantity * ws_list_price) > (SELECT average_sales
FROM avg_sales)) y
GROUP BY rollup ( channel, i_brand_id, i_class_id, i_category_id )
ORDER BY channel,
i_brand_id,
i_class_id,
i_category_id
LIMIT 100;
WITH cross_items
AS (SELECT i_item_sk ss_item_sk
FROM item,
(SELECT iss.i_brand_id brand_id,
iss.i_class_id class_id,
iss.i_category_id category_id
FROM store_sales,
item iss,
date_dim d1
WHERE ss_item_sk = iss.i_item_sk
AND ss_sold_date_sk = d1.d_date_sk
AND d1.d_year BETWEEN 1999 AND 1999 + 2
INTERSECT
SELECT ics.i_brand_id,
ics.i_class_id,
ics.i_category_id
FROM catalog_sales,
item ics,
date_dim d2
WHERE cs_item_sk = ics.i_item_sk
AND cs_sold_date_sk = d2.d_date_sk
AND d2.d_year BETWEEN 1999 AND 1999 + 2
INTERSECT
SELECT iws.i_brand_id,
iws.i_class_id,
iws.i_category_id
FROM web_sales,
item iws,
date_dim d3
WHERE ws_item_sk = iws.i_item_sk
AND ws_sold_date_sk = d3.d_date_sk
AND d3.d_year BETWEEN 1999 AND 1999 + 2) x
WHERE i_brand_id = brand_id
AND i_class_id = class_id
AND i_category_id = category_id),
avg_sales
AS (SELECT Avg(quantity * list_price) average_sales
FROM (SELECT ss_quantity quantity,
ss_list_price list_price
FROM store_sales,
date_dim
WHERE ss_sold_date_sk = d_date_sk
AND d_year BETWEEN 1999 AND 1999 + 2
UNION ALL
SELECT cs_quantity quantity,
cs_list_price list_price
FROM catalog_sales,
date_dim
WHERE cs_sold_date_sk = d_date_sk
AND d_year BETWEEN 1999 AND 1999 + 2
UNION ALL
SELECT ws_quantity quantity,
ws_list_price list_price
FROM web_sales,
date_dim
WHERE ws_sold_date_sk = d_date_sk
AND d_year BETWEEN 1999 AND 1999 + 2) x)
SELECT *
FROM (SELECT 'store' channel,
i_brand_id,
i_class_id,
i_category_id,
Sum(ss_quantity * ss_list_price) sales,
Count(*) number_sales
FROM store_sales,
item,
date_dim
WHERE ss_item_sk IN (SELECT ss_item_sk
FROM cross_items)
AND ss_item_sk = i_item_sk
AND ss_sold_date_sk = d_date_sk
AND d_week_seq = (SELECT d_week_seq
FROM date_dim
WHERE d_year = 1999 + 1
AND d_moy = 12
AND d_dom = 25)
GROUP BY i_brand_id,
i_class_id,
i_category_id
HAVING Sum(ss_quantity * ss_list_price) > (SELECT average_sales
FROM avg_sales)) this_year,
(SELECT 'store' channel,
i_brand_id,
i_class_id,
i_category_id,
Sum(ss_quantity * ss_list_price) sales,
Count(*) number_sales
FROM store_sales,
item,
date_dim
WHERE ss_item_sk IN (SELECT ss_item_sk
FROM cross_items)
AND ss_item_sk = i_item_sk
AND ss_sold_date_sk = d_date_sk
AND d_week_seq = (SELECT d_week_seq
FROM date_dim
WHERE d_year = 1999
AND d_moy = 12
AND d_dom = 25)
GROUP BY i_brand_id,
i_class_id,
i_category_id
HAVING Sum(ss_quantity * ss_list_price) > (SELECT average_sales
FROM avg_sales)) last_year
WHERE this_year.i_brand_id = last_year.i_brand_id
AND this_year.i_class_id = last_year.i_class_id
AND this_year.i_category_id = last_year.i_category_id
ORDER BY this_year.channel,
this_year.i_brand_id,
this_year.i_class_id,
this_year.i_category_id
LIMIT 100;