-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0_transform_summarize.sql
More file actions
348 lines (346 loc) · 12.2 KB
/
Copy path0_transform_summarize.sql
File metadata and controls
348 lines (346 loc) · 12.2 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
CREATE TEMP FUNCTION
ClearCurrency(x STRING)
RETURNS FLOAT64 AS (
CASE
WHEN x LIKE '%$ -%' THEN NULL
ELSE
CAST(REGEXP_REPLACE(x,r'[^0-9.]+','') AS FLOAT64)
END
);
CREATE TEMPORARY TABLE dup_data AS
SELECT
UPPER(Shipping_Warehouse) AS shipper_wh,
Assigned_Customer_Warehouse AS assigned_cust_wh,
Base_T_L_H_Nbr AS base_tlh,
SO____T_R_ AS soid_trid,
Customer AS consignee_id,
custname AS consignee_name,
shipdate AS shipped_date,
Zip AS zip,
Has_Stops_Over AS has_stops_over,
ClearCurrency(_Contract__Freight_) AS contract_price,
ClearCurrency(_Freight_Amount_) AS freight_price,
ClearCurrency(_Discrepancy_Amount_) AS discrepancy_amount,
ClearCurrency(_Grand_Total_) AS final_price,
Posted AS was_posted,
Customer_PO_Number AS cust_po,
UPPER(Carrier) AS carrier,
CAST(REPLACE(Shipment_,'-SH','' ) AS INT64) AS load_id,
State AS state
FROM
`potrtms.kellogg.raw_data`
;
CREATE OR REPLACE TABLE
`potrtms.kellogg.data` AS
WITH
repeated AS (
SELECT
load_id AS repeated_load_id,
COUNT(*) AS count
FROM
dup_data
GROUP BY
load_id
HAVING
count > 1 )
SELECT
* EXCEPT (repeated_load_id,
count)
FROM
dup_data
LEFT JOIN
repeated
ON
repeated_load_id = load_id
WHERE
repeated_load_id IS NULL
AND final_price IS NOT NULL
;
CREATE TEMPORARY TABLE dup_accessorials AS
SELECT
UPPER(Shipping_Warehouse) AS shipper_wh,
Assigned_Customer_Warehouse AS assigned_cust_wh,
Base_T_L_H_Nbr AS base_tlh,
SO____T_R_ AS soid_trid,
Customer AS consignee_id,
custname AS consignee_name,
shipdate AS shipped_date,
Zip AS zip,
Has_Stops_Over AS has_stops_over,
ClearCurrency(_Contract__Freight_) AS contract_price,
ClearCurrency(_Freight_Amount_) AS freight_price,
ClearCurrency(_Discrepancy_Amount_) AS discrepancy_amount,
ClearCurrency(_Grand_Total_) AS final_price,
Posted AS was_posted,
Customer_PO_Number AS cust_po,
UPPER(Carrier) AS carrier,
CAST(REPLACE(Shipment_,'-SH','' ) AS INT64) AS load_id,
State AS state
FROM
`potrtms.kellogg.raw_accessorials`
;
CREATE OR REPLACE TABLE
`potrtms.kellogg.accessorials` AS
WITH
repeated AS (
SELECT
load_id AS repeated_load_id,
COUNT(*) AS count
FROM
dup_accessorials
GROUP BY
load_id
HAVING
count > 1 )
SELECT
* EXCEPT (repeated_load_id,
count)
FROM
dup_accessorials
LEFT JOIN
repeated
ON
repeated_load_id = load_id
WHERE
repeated_load_id IS NULL
AND final_price IS NOT NULL
;
CREATE TEMPORARY TABLE dup_zip_to_zip AS
SELECT
Customer AS consignee_id,
custname AS consignee_name,
shipdate AS shipped_date,
Ship_Zip AS shipper_zip,
Deliver_Zip AS consignee_zip,
Has_Stops_Over AS has_stops_over,
ClearCurrency(_Contract__Freight_) AS contract_price,
ClearCurrency(_Freight_Amount_) AS freight_price,
ClearCurrency(_Discrepancy_Amount_) AS discrepancy_amount,
ClearCurrency(_Grand_Total_) AS final_price,
Posted AS was_posted,
Customer_PO_Number AS cust_po,
UPPER(Carrier) AS carrier,
CAST(REPLACE(Shipment_,'-SH','' ) AS INT64) AS load_id,
State AS state
FROM
`potrtms.kellogg.raw_zip_to_zip`
;
CREATE OR REPLACE TABLE
`potrtms.kellogg.zip_to_zip` AS
WITH
repeated AS (
SELECT
load_id AS repeated_load_id,
COUNT(*) AS count
FROM
dup_zip_to_zip
GROUP BY
load_id
HAVING
count > 1 )
SELECT
* EXCEPT (repeated_load_id,
count)
FROM
dup_zip_to_zip
LEFT JOIN
repeated
ON
repeated_load_id = load_id
WHERE
repeated_load_id IS NULL
AND final_price IS NOT NULL
;
WITH
summary_data AS (
SELECT
COUNT(*) row_count,
--COUNT(DISTINCT shipper_wh) dstc_shipper_wh,
--SUM(CASE WHEN shipper_wh IS NULL THEN 1 END) null_shipper_wh,
--COUNT(DISTINCT assigned_cust_wh) dstc_assigned_cust_wh,
--SUM(CASE WHEN assigned_cust_wh IS NULL THEN 1 END) null_assigned_cust_wh,
--COUNT(DISTINCT base_tlh) dstc_base_tlh,
--SUM(CASE WHEN base_tlh IS NULL THEN 1 END) null_base_tlh,
--MIN(base_tlh) min_base_tlh,
--MAX(base_tlh) max_base_tlh,
--AVG(base_tlh) avg_base_tlh,
--COUNT(DISTINCT soid_trid) dstc_soid_trid,
--SUM(CASE WHEN soid_trid IS NULL THEN 1 END) null_soid_trid,
COUNT(DISTINCT consignee_id) dstc_consignee_id,
SUM(CASE WHEN consignee_id IS NULL THEN 1 END) null_consignee_id,
MIN(consignee_id) min_consignee_id,
MAX(consignee_id) max_consignee_id,
COUNT(DISTINCT consignee_name) dstc_consignee_name,
SUM(CASE WHEN consignee_name IS NULL THEN 1 END) null_consignee_name,
SUM(CASE WHEN shipped_date IS NULL THEN 1 END) null_shipped_date,
MIN(shipped_date) min_shipped_date,
MAX(shipped_date) max_shipped_date,
--COUNT(DISTINCT zip) dstc_zip,
--SUM(CASE WHEN zip IS NULL THEN 1 END) null_zip,
COUNT(DISTINCT has_stops_over) dstc_has_stops_over,
SUM(CASE WHEN has_stops_over IS NULL THEN 1 END) null_has_stops_over,
COUNT(DISTINCT contract_price) dstc_contract_price,
SUM(CASE WHEN contract_price IS NULL THEN 1 END) null_contract_price,
MIN(contract_price) min_contract_price,
MAX(contract_price) max_contract_price,
AVG(contract_price) avg_contract_price,
COUNT(DISTINCT freight_price) dstc_freight_price,
SUM(CASE WHEN freight_price IS NULL THEN 1 END) null_freight_price,
MIN(freight_price) min_freight_price,
MAX(freight_price) max_freight_price,
AVG(freight_price) avg_freight_price,
COUNT(DISTINCT discrepancy_amount) dstc_discrepancy_amount,
SUM(CASE WHEN discrepancy_amount IS NULL THEN 1 END) null_discrepancy_amount,
MIN(discrepancy_amount) min_discrepancy_amount,
MAX(discrepancy_amount) max_discrepancy_amount,
AVG(discrepancy_amount) avg_discrepancy_amount,
COUNT(DISTINCT final_price) dstc_final_price,
SUM(CASE WHEN final_price IS NULL THEN 1 END) null_final_price,
MIN(final_price) min_final_price,
MAX(final_price) max_final_price,
AVG(final_price) avg_final_price,
COUNT(DISTINCT was_posted) dstc_was_posted,
SUM(CASE WHEN was_posted IS NULL THEN 1 END) null_was_posted,
COUNT(DISTINCT cust_po) dstc_cust_po,
SUM(CASE WHEN cust_po IS NULL THEN 1 END) null_cust_po,
COUNT(DISTINCT carrier) dstc_carrier,
SUM(CASE WHEN carrier IS NULL THEN 1 END) null_carrier,
COUNT(DISTINCT load_id) dstc_load_id,
SUM(CASE WHEN load_id IS NULL THEN 1 END) null_load_id,
COUNT(DISTINCT state) dstc_state,
SUM(CASE WHEN state IS NULL THEN 1 END) null_state,
FROM
`potrtms.kellogg.data`
),
summary_accessorials AS (
SELECT
COUNT(*) row_count,
--COUNT(DISTINCT shipper_wh) dstc_shipper_wh,
--SUM(CASE WHEN shipper_wh IS NULL THEN 1 END) null_shipper_wh,
--COUNT(DISTINCT assigned_cust_wh) dstc_assigned_cust_wh,
--SUM(CASE WHEN assigned_cust_wh IS NULL THEN 1 END) null_assigned_cust_wh,
--COUNT(DISTINCT base_tlh) dstc_base_tlh,
--SUM(CASE WHEN base_tlh IS NULL THEN 1 END) null_base_tlh,
--MIN(base_tlh) min_base_tlh,
--MAX(base_tlh) max_base_tlh,
--AVG(base_tlh) avg_base_tlh,
--COUNT(DISTINCT soid_trid) dstc_soid_trid,
--SUM(CASE WHEN soid_trid IS NULL THEN 1 END) null_soid_trid,
COUNT(DISTINCT consignee_id) dstc_consignee_id,
SUM(CASE WHEN consignee_id IS NULL THEN 1 END) null_consignee_id,
MIN(consignee_id) min_consignee_id,
MAX(consignee_id) max_consignee_id,
COUNT(DISTINCT consignee_name) dstc_consignee_name,
SUM(CASE WHEN consignee_name IS NULL THEN 1 END) null_consignee_name,
SUM(CASE WHEN shipped_date IS NULL THEN 1 END) null_shipped_date,
MIN(shipped_date) min_shipped_date,
MAX(shipped_date) max_shipped_date,
--COUNT(DISTINCT zip) dstc_zip,
--SUM(CASE WHEN zip IS NULL THEN 1 END) null_zip,
COUNT(DISTINCT has_stops_over) dstc_has_stops_over,
SUM(CASE WHEN has_stops_over IS NULL THEN 1 END) null_has_stops_over,
COUNT(DISTINCT contract_price) dstc_contract_price,
SUM(CASE WHEN contract_price IS NULL THEN 1 END) null_contract_price,
MIN(contract_price) min_contract_price,
MAX(contract_price) max_contract_price,
AVG(contract_price) avg_contract_price,
COUNT(DISTINCT freight_price) dstc_freight_price,
SUM(CASE WHEN freight_price IS NULL THEN 1 END) null_freight_price,
MIN(freight_price) min_freight_price,
MAX(freight_price) max_freight_price,
AVG(freight_price) avg_freight_price,
COUNT(DISTINCT discrepancy_amount) dstc_discrepancy_amount,
SUM(CASE WHEN discrepancy_amount IS NULL THEN 1 END) null_discrepancy_amount,
MIN(discrepancy_amount) min_discrepancy_amount,
MAX(discrepancy_amount) max_discrepancy_amount,
AVG(discrepancy_amount) avg_discrepancy_amount,
COUNT(DISTINCT final_price) dstc_final_price,
SUM(CASE WHEN final_price IS NULL THEN 1 END) null_final_price,
MIN(final_price) min_final_price,
MAX(final_price) max_final_price,
AVG(final_price) avg_final_price,
COUNT(DISTINCT was_posted) dstc_was_posted,
SUM(CASE WHEN was_posted IS NULL THEN 1 END) null_was_posted,
COUNT(DISTINCT cust_po) dstc_cust_po,
SUM(CASE WHEN cust_po IS NULL THEN 1 END) null_cust_po,
COUNT(DISTINCT carrier) dstc_carrier,
SUM(CASE WHEN carrier IS NULL THEN 1 END) null_carrier,
COUNT(DISTINCT load_id) dstc_load_id,
SUM(CASE WHEN load_id IS NULL THEN 1 END) null_load_id,
COUNT(DISTINCT state) dstc_state,
SUM(CASE WHEN state IS NULL THEN 1 END) null_state,
FROM
`potrtms.kellogg.accessorials` ),
summary_zip AS (
SELECT
COUNT(*) row_count,
--COUNT(DISTINCT shipper_wh) dstc_shipper_wh,
--SUM(CASE WHEN shipper_wh IS NULL THEN 1 END) null_shipper_wh,
--COUNT(DISTINCT assigned_cust_wh) dstc_assigned_cust_wh,
--SUM(CASE WHEN assigned_cust_wh IS NULL THEN 1 END) null_assigned_cust_wh,
--COUNT(DISTINCT base_tlh) dstc_base_tlh,
--SUM(CASE WHEN base_tlh IS NULL THEN 1 END) null_base_tlh,
--MIN(base_tlh) min_base_tlh,
--MAX(base_tlh) max_base_tlh,
--AVG(base_tlh) avg_base_tlh,
--COUNT(DISTINCT soid_trid) dstc_soid_trid,
--SUM(CASE WHEN soid_trid IS NULL THEN 1 END) null_soid_trid,
COUNT(DISTINCT consignee_id) dstc_consignee_id,
SUM(CASE WHEN consignee_id IS NULL THEN 1 END) null_consignee_id,
MIN(consignee_id) min_consignee_id,
MAX(consignee_id) max_consignee_id,
COUNT(DISTINCT consignee_name) dstc_consignee_name,
SUM(CASE WHEN consignee_name IS NULL THEN 1 END) null_consignee_name,
SUM(CASE WHEN shipped_date IS NULL THEN 1 END) null_shipped_date,
MIN(shipped_date) min_shipped_date,
MAX(shipped_date) max_shipped_date,
--COUNT(DISTINCT zip) dstc_zip,
--SUM(CASE WHEN zip IS NULL THEN 1 END) null_zip,
COUNT(DISTINCT has_stops_over) dstc_has_stops_over,
SUM(CASE WHEN has_stops_over IS NULL THEN 1 END) null_has_stops_over,
COUNT(DISTINCT contract_price) dstc_contract_price,
SUM(CASE WHEN contract_price IS NULL THEN 1 END) null_contract_price,
MIN(contract_price) min_contract_price,
MAX(contract_price) max_contract_price,
AVG(contract_price) avg_contract_price,
COUNT(DISTINCT freight_price) dstc_freight_price,
SUM(CASE WHEN freight_price IS NULL THEN 1 END) null_freight_price,
MIN(freight_price) min_freight_price,
MAX(freight_price) max_freight_price,
AVG(freight_price) avg_freight_price,
COUNT(DISTINCT discrepancy_amount) dstc_discrepancy_amount,
SUM(CASE WHEN discrepancy_amount IS NULL THEN 1 END) null_discrepancy_amount,
MIN(discrepancy_amount) min_discrepancy_amount,
MAX(discrepancy_amount) max_discrepancy_amount,
AVG(discrepancy_amount) avg_discrepancy_amount,
COUNT(DISTINCT final_price) dstc_final_price,
SUM(CASE WHEN final_price IS NULL THEN 1 END) null_final_price,
MIN(final_price) min_final_price,
MAX(final_price) max_final_price,
AVG(final_price) avg_final_price,
COUNT(DISTINCT was_posted) dstc_was_posted,
SUM(CASE WHEN was_posted IS NULL THEN 1 END) null_was_posted,
COUNT(DISTINCT cust_po) dstc_cust_po,
SUM(CASE WHEN cust_po IS NULL THEN 1 END) null_cust_po,
COUNT(DISTINCT carrier) dstc_carrier,
SUM(CASE WHEN carrier IS NULL THEN 1 END) null_carrier,
COUNT(DISTINCT load_id) dstc_load_id,
SUM(CASE WHEN load_id IS NULL THEN 1 END) null_load_id,
COUNT(DISTINCT state) dstc_state,
SUM(CASE WHEN state IS NULL THEN 1 END) null_state,
FROM
`potrtms.kellogg.zip_to_zip` )
SELECT
*
FROM
summary_accessorials
UNION ALL
SELECT
*
FROM
summary_data
UNION ALL
SELECT
*
FROM summary_zip
;