-
Notifications
You must be signed in to change notification settings - Fork 10
/
q66.sql
240 lines (240 loc) · 8.65 KB
/
q66.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
SELECT
w_warehouse_name,
w_warehouse_sq_ft,
w_city,
w_county,
w_state,
w_country,
ship_carriers,
year,
sum(jan_sales) AS jan_sales,
sum(feb_sales) AS feb_sales,
sum(mar_sales) AS mar_sales,
sum(apr_sales) AS apr_sales,
sum(may_sales) AS may_sales,
sum(jun_sales) AS jun_sales,
sum(jul_sales) AS jul_sales,
sum(aug_sales) AS aug_sales,
sum(sep_sales) AS sep_sales,
sum(oct_sales) AS oct_sales,
sum(nov_sales) AS nov_sales,
sum(dec_sales) AS dec_sales,
sum(jan_sales / w_warehouse_sq_ft) AS jan_sales_per_sq_foot,
sum(feb_sales / w_warehouse_sq_ft) AS feb_sales_per_sq_foot,
sum(mar_sales / w_warehouse_sq_ft) AS mar_sales_per_sq_foot,
sum(apr_sales / w_warehouse_sq_ft) AS apr_sales_per_sq_foot,
sum(may_sales / w_warehouse_sq_ft) AS may_sales_per_sq_foot,
sum(jun_sales / w_warehouse_sq_ft) AS jun_sales_per_sq_foot,
sum(jul_sales / w_warehouse_sq_ft) AS jul_sales_per_sq_foot,
sum(aug_sales / w_warehouse_sq_ft) AS aug_sales_per_sq_foot,
sum(sep_sales / w_warehouse_sq_ft) AS sep_sales_per_sq_foot,
sum(oct_sales / w_warehouse_sq_ft) AS oct_sales_per_sq_foot,
sum(nov_sales / w_warehouse_sq_ft) AS nov_sales_per_sq_foot,
sum(dec_sales / w_warehouse_sq_ft) AS dec_sales_per_sq_foot,
sum(jan_net) AS jan_net,
sum(feb_net) AS feb_net,
sum(mar_net) AS mar_net,
sum(apr_net) AS apr_net,
sum(may_net) AS may_net,
sum(jun_net) AS jun_net,
sum(jul_net) AS jul_net,
sum(aug_net) AS aug_net,
sum(sep_net) AS sep_net,
sum(oct_net) AS oct_net,
sum(nov_net) AS nov_net,
sum(dec_net) AS dec_net
FROM (
(SELECT
w_warehouse_name,
w_warehouse_sq_ft,
w_city,
w_county,
w_state,
w_country,
concat('DHL', ',', 'BARIAN') AS ship_carriers,
d_year AS year,
sum(CASE WHEN d_moy = 1
THEN ws_ext_sales_price * ws_quantity
ELSE 0 END) AS jan_sales,
sum(CASE WHEN d_moy = 2
THEN ws_ext_sales_price * ws_quantity
ELSE 0 END) AS feb_sales,
sum(CASE WHEN d_moy = 3
THEN ws_ext_sales_price * ws_quantity
ELSE 0 END) AS mar_sales,
sum(CASE WHEN d_moy = 4
THEN ws_ext_sales_price * ws_quantity
ELSE 0 END) AS apr_sales,
sum(CASE WHEN d_moy = 5
THEN ws_ext_sales_price * ws_quantity
ELSE 0 END) AS may_sales,
sum(CASE WHEN d_moy = 6
THEN ws_ext_sales_price * ws_quantity
ELSE 0 END) AS jun_sales,
sum(CASE WHEN d_moy = 7
THEN ws_ext_sales_price * ws_quantity
ELSE 0 END) AS jul_sales,
sum(CASE WHEN d_moy = 8
THEN ws_ext_sales_price * ws_quantity
ELSE 0 END) AS aug_sales,
sum(CASE WHEN d_moy = 9
THEN ws_ext_sales_price * ws_quantity
ELSE 0 END) AS sep_sales,
sum(CASE WHEN d_moy = 10
THEN ws_ext_sales_price * ws_quantity
ELSE 0 END) AS oct_sales,
sum(CASE WHEN d_moy = 11
THEN ws_ext_sales_price * ws_quantity
ELSE 0 END) AS nov_sales,
sum(CASE WHEN d_moy = 12
THEN ws_ext_sales_price * ws_quantity
ELSE 0 END) AS dec_sales,
sum(CASE WHEN d_moy = 1
THEN ws_net_paid * ws_quantity
ELSE 0 END) AS jan_net,
sum(CASE WHEN d_moy = 2
THEN ws_net_paid * ws_quantity
ELSE 0 END) AS feb_net,
sum(CASE WHEN d_moy = 3
THEN ws_net_paid * ws_quantity
ELSE 0 END) AS mar_net,
sum(CASE WHEN d_moy = 4
THEN ws_net_paid * ws_quantity
ELSE 0 END) AS apr_net,
sum(CASE WHEN d_moy = 5
THEN ws_net_paid * ws_quantity
ELSE 0 END) AS may_net,
sum(CASE WHEN d_moy = 6
THEN ws_net_paid * ws_quantity
ELSE 0 END) AS jun_net,
sum(CASE WHEN d_moy = 7
THEN ws_net_paid * ws_quantity
ELSE 0 END) AS jul_net,
sum(CASE WHEN d_moy = 8
THEN ws_net_paid * ws_quantity
ELSE 0 END) AS aug_net,
sum(CASE WHEN d_moy = 9
THEN ws_net_paid * ws_quantity
ELSE 0 END) AS sep_net,
sum(CASE WHEN d_moy = 10
THEN ws_net_paid * ws_quantity
ELSE 0 END) AS oct_net,
sum(CASE WHEN d_moy = 11
THEN ws_net_paid * ws_quantity
ELSE 0 END) AS nov_net,
sum(CASE WHEN d_moy = 12
THEN ws_net_paid * ws_quantity
ELSE 0 END) AS dec_net
FROM
web_sales, warehouse, date_dim, time_dim, ship_mode
WHERE
ws_warehouse_sk = w_warehouse_sk
AND ws_sold_date_sk = d_date_sk
AND ws_sold_time_sk = t_time_sk
AND ws_ship_mode_sk = sm_ship_mode_sk
AND d_year = 2001
AND t_time BETWEEN 30838 AND 30838 + 28800
AND sm_carrier IN ('DHL', 'BARIAN')
GROUP BY
w_warehouse_name, w_warehouse_sq_ft, w_city, w_county, w_state, w_country, d_year)
UNION ALL
(SELECT
w_warehouse_name,
w_warehouse_sq_ft,
w_city,
w_county,
w_state,
w_country,
concat('DHL', ',', 'BARIAN') AS ship_carriers,
d_year AS year,
sum(CASE WHEN d_moy = 1
THEN cs_sales_price * cs_quantity
ELSE 0 END) AS jan_sales,
sum(CASE WHEN d_moy = 2
THEN cs_sales_price * cs_quantity
ELSE 0 END) AS feb_sales,
sum(CASE WHEN d_moy = 3
THEN cs_sales_price * cs_quantity
ELSE 0 END) AS mar_sales,
sum(CASE WHEN d_moy = 4
THEN cs_sales_price * cs_quantity
ELSE 0 END) AS apr_sales,
sum(CASE WHEN d_moy = 5
THEN cs_sales_price * cs_quantity
ELSE 0 END) AS may_sales,
sum(CASE WHEN d_moy = 6
THEN cs_sales_price * cs_quantity
ELSE 0 END) AS jun_sales,
sum(CASE WHEN d_moy = 7
THEN cs_sales_price * cs_quantity
ELSE 0 END) AS jul_sales,
sum(CASE WHEN d_moy = 8
THEN cs_sales_price * cs_quantity
ELSE 0 END) AS aug_sales,
sum(CASE WHEN d_moy = 9
THEN cs_sales_price * cs_quantity
ELSE 0 END) AS sep_sales,
sum(CASE WHEN d_moy = 10
THEN cs_sales_price * cs_quantity
ELSE 0 END) AS oct_sales,
sum(CASE WHEN d_moy = 11
THEN cs_sales_price * cs_quantity
ELSE 0 END) AS nov_sales,
sum(CASE WHEN d_moy = 12
THEN cs_sales_price * cs_quantity
ELSE 0 END) AS dec_sales,
sum(CASE WHEN d_moy = 1
THEN cs_net_paid_inc_tax * cs_quantity
ELSE 0 END) AS jan_net,
sum(CASE WHEN d_moy = 2
THEN cs_net_paid_inc_tax * cs_quantity
ELSE 0 END) AS feb_net,
sum(CASE WHEN d_moy = 3
THEN cs_net_paid_inc_tax * cs_quantity
ELSE 0 END) AS mar_net,
sum(CASE WHEN d_moy = 4
THEN cs_net_paid_inc_tax * cs_quantity
ELSE 0 END) AS apr_net,
sum(CASE WHEN d_moy = 5
THEN cs_net_paid_inc_tax * cs_quantity
ELSE 0 END) AS may_net,
sum(CASE WHEN d_moy = 6
THEN cs_net_paid_inc_tax * cs_quantity
ELSE 0 END) AS jun_net,
sum(CASE WHEN d_moy = 7
THEN cs_net_paid_inc_tax * cs_quantity
ELSE 0 END) AS jul_net,
sum(CASE WHEN d_moy = 8
THEN cs_net_paid_inc_tax * cs_quantity
ELSE 0 END) AS aug_net,
sum(CASE WHEN d_moy = 9
THEN cs_net_paid_inc_tax * cs_quantity
ELSE 0 END) AS sep_net,
sum(CASE WHEN d_moy = 10
THEN cs_net_paid_inc_tax * cs_quantity
ELSE 0 END) AS oct_net,
sum(CASE WHEN d_moy = 11
THEN cs_net_paid_inc_tax * cs_quantity
ELSE 0 END) AS nov_net,
sum(CASE WHEN d_moy = 12
THEN cs_net_paid_inc_tax * cs_quantity
ELSE 0 END) AS dec_net
FROM
catalog_sales, warehouse, date_dim, time_dim, ship_mode
WHERE
cs_warehouse_sk = w_warehouse_sk
AND cs_sold_date_sk = d_date_sk
AND cs_sold_time_sk = t_time_sk
AND cs_ship_mode_sk = sm_ship_mode_sk
AND d_year = 2001
AND t_time BETWEEN 30838 AND 30838 + 28800
AND sm_carrier IN ('DHL', 'BARIAN')
GROUP BY
w_warehouse_name, w_warehouse_sq_ft, w_city, w_county, w_state, w_country, d_year
)
) x
GROUP BY
w_warehouse_name, w_warehouse_sq_ft, w_city, w_county, w_state, w_country,
ship_carriers, year
ORDER BY w_warehouse_name
LIMIT 100;