-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries_PIM.py
More file actions
448 lines (355 loc) · 16 KB
/
queries_PIM.py
File metadata and controls
448 lines (355 loc) · 16 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 12 12:56:37 2019
@author: xcxg109
"""
gws_basic_query="""
SELECT
tprod."gtPartNumber" as "GWS_SKU"
, tprod."gtPartNumber" as "Grainger_SKU"
, tprod."categoryId" AS "GWS_Node_ID"
FROM taxonomy_product tprod
WHERE {} IN ({})
"""
gws_attr_query="""
WITH RECURSIVE tax AS (
SELECT id,
name,
ARRAY[]::INTEGER[] AS ancestors,
ARRAY[]::character varying[] AS ancestor_names
FROM taxonomy_category as category
WHERE "parentId" IS NULL
AND category.deleted = false
UNION ALL
SELECT category.id,
category.name,
tax.ancestors || tax.id,
tax.ancestor_names || tax.name
FROM taxonomy_category as category
INNER JOIN tax ON category."parentId" = tax.id
WHERE category.deleted = false
)
SELECT
array_to_string(tax.ancestor_names || tax.name,' > ') as "GWS_PIM_Path"
, tax.ancestors[1] as "GWS_Category_ID"
, tax.ancestor_names[1] as "GWS_Category_Name"
, tax_att."categoryId" AS "GWS_Node_ID"
, tax.name as "GWS_Node_Name"
, tax_att.id as "GWS_Attr_ID"
, tax_att.name as "GWS_Attribute_Name"
, tax_att.description as "GWS_Attribute_Definition"
FROM taxonomy_attribute tax_att
INNER JOIN tax
ON tax.id = tax_att."categoryId"
WHERE {} IN ({})
"""
gws_attr_values="""
WITH RECURSIVE tax AS (
SELECT id,
name,
ARRAY[]::INTEGER[] AS ancestors,
ARRAY[]::character varying[] AS ancestor_names
FROM taxonomy_category as category
WHERE "parentId" IS NULL
AND category.deleted = false
UNION ALL
SELECT category.id,
category.name,
tax.ancestors || tax.id,
tax.ancestor_names || tax.name
FROM taxonomy_category as category
INNER JOIN tax ON category."parentId" = tax.id
WHERE category.deleted = false
)
SELECT
array_to_string(tax.ancestor_names || tax.name,' > ') as "GWS_PIM_Path"
, tax.ancestors[1] as "GWS_Category_ID"
, tax.ancestor_names[1] as "GWS_Category_Name"
, tprod."categoryId" AS "GWS_Node_ID"
, tax.name as "GWS_Node_Name"
, tprod."gtPartNumber" as "GWS_SKU"
, tprod."gtPartNumber" as "Grainger_SKU"
, tax_att.id as "GWS_Attr_ID"
, tax_att.name as "GWS_Attribute_Name"
, tax_att.description as "GWS_Attribute_Definition"
, tprodvalue.value as "Original Value"
, tprodvalue."valueNormalized" as "Normalized Value"
, tax_att."unitGroupId" as "Unit_Group_ID"
FROM taxonomy_product tprod
INNER JOIN tax
ON tax.id = tprod."categoryId"
-- AND (4458 = ANY(tax.ancestors)) --OR 8215 = ANY(tax.ancestors) OR 7739 = ANY(tax.ancestors)) -- *** ADD TOP LEVEL NODES HERE ***
INNER JOIN taxonomy_attribute tax_att
ON tax_att."categoryId" = tprod."categoryId"
INNER JOIN taxonomy_product_attribute_value tprodvalue
ON tprod.id = tprodvalue."productId"
AND tax_att.id = tprodvalue."attributeId"
WHERE {} IN ({})
"""
#get basic SKU list and hierarchy data from Grainger teradata material universe
gws_hier_query="""
WITH RECURSIVE tax AS (
SELECT id,
name,
ARRAY[]::INTEGER[] AS ancestors,
ARRAY[]::character varying[] AS ancestor_names
FROM taxonomy_category as category
WHERE "parentId" IS NULL
AND category.deleted = false
UNION ALL
SELECT category.id,
category.name,
tax.ancestors || category."parentId",
tax.ancestor_names || parent_category.name
FROM taxonomy_category as category
JOIN tax on category."parentId" = tax.id
JOIN taxonomy_category parent_category on category."parentId" = parent_category.id
WHERE category.deleted = false
)
SELECT
array_to_string(tax.ancestor_names || tax.name,' > ') as "GWS_PIM_Path"
, tax.ancestors[1] as "GWS_Category_ID"
, tax.ancestor_names[1] as "GWS_Category_Name"
, tprod."categoryId" AS "GWS_Node_ID"
, tax.name as "GWS_Node_Name"
, tprod."gtPartNumber" as "GWS_SKU"
, tprod."gtPartNumber" as "Grainger_SKU"
FROM taxonomy_product tprod
INNER JOIN tax
ON tax.id = tprod."categoryId"
-- AND (10006 = ANY(tax.ancestors)) --OR 8215 = ANY(tax.ancestors) OR 7739 = ANY(tax.ancestors)) -- *** ADD TOP LEVEL NODES HERE ***
WHERE {} IN ({})
"""
#pull attribute values from Grainger teradata material universe by L3
grainger_attr_query="""
SELECT cat.SEGMENT_ID AS Segment_ID
, cat.SEGMENT_NAME AS Segment_Name
, cat.FAMILY_ID AS Family_ID
, cat.FAMILY_NAME AS Family_Name
, cat.CATEGORY_ID AS Category_ID
, cat.CATEGORY_NAME AS Category_Name
, item.MATERIAL_NO AS Grainger_SKU
, attr.DESCRIPTOR_ID as Grainger_Attr_ID
, attr.DESCRIPTOR_NAME as Grainger_Attribute_Name
, item_attr.ITEM_DESC_VALUE as Grainger_Attribute_Value
, cat_desc.ENDECA_RANKING
, item.PM_CODE AS PM_Code
, item.SALES_STATUS
, attr.attribute_level_definition as Grainger_Attribute_Definition
, cat_desc.cat_specific_attr_definition as Grainger_Category_Specific_Definition
FROM PRD_DWH_VIEW_MTRL.ITEM_DESC_V AS item_attr
INNER JOIN PRD_DWH_VIEW_MTRL.ITEM_V AS item
ON item_attr.MATERIAL_NO = item.MATERIAL_NO
AND item.DELETED_FLAG = 'N'
AND item_attr.DELETED_FLAG = 'N'
AND item_attr.LANG = 'EN'
AND item.PRODUCT_APPROVED_US_FLAG = 'Y'
INNER JOIN PRD_DWH_VIEW_MTRL.CATEGORY_V AS cat
ON cat.CATEGORY_ID = item_attr.CATEGORY_ID
AND item_attr.DELETED_FLAG = 'N'
AND item.PM_CODE NOT IN ('R9')
AND item.PM_CODE NOT IN ('R4')
INNER JOIN PRD_DWH_VIEW_MTRL.CAT_DESC_V AS cat_desc
ON cat_desc.CATEGORY_ID = item_attr.CATEGORY_ID
AND cat_desc.DESCRIPTOR_ID = item_attr.DESCRIPTOR_ID
AND cat_desc.DELETED_FLAG='N'
INNER JOIN PRD_DWH_VIEW_MTRL.MAT_DESCRIPTOR_V AS attr
ON attr.DESCRIPTOR_ID = item_attr.DESCRIPTOR_ID
AND attr.DELETED_FLAG = 'N'
WHERE item.SALES_STATUS NOT IN ('DG', 'DV', 'WV', 'WG')
AND {} IN ({})
"""
grainger_name_query="""
SELECT cat.SEGMENT_NAME AS Segment_ID
, cat.FAMILY_NAME AS Family_Name
, cat.CATEGORY_NAME AS Category_Name
, cat.CATEGORY_ID AS Category_ID
, item.MATERIAL_NO AS Grainger_SKU
, attr.DESCRIPTOR_NAME AS Grainger_Attribute_Name
, attr.DESCRIPTOR_ID as Grainger_Attr_ID
FROM PRD_DWH_VIEW_MTRL.ITEM_DESC_V AS item_attr
INNER JOIN PRD_DWH_VIEW_MTRL.ITEM_V AS item
ON item_attr.MATERIAL_NO = item.MATERIAL_NO
AND item.DELETED_FLAG = 'N'
AND item_attr.DELETED_FLAG = 'N'
AND item_attr.LANG = 'EN'
AND item.PRODUCT_APPROVED_US_FLAG = 'Y'
INNER JOIN PRD_DWH_VIEW_MTRL.CATEGORY_V AS cat
ON cat.CATEGORY_ID = item_attr.CATEGORY_ID
AND item_attr.DELETED_FLAG = 'N'
AND item.PM_CODE NOT IN ('R9')
INNER JOIN PRD_DWH_VIEW_MTRL.CAT_DESC_V AS cat_desc
ON cat_desc.CATEGORY_ID = item_attr.CATEGORY_ID
AND cat_desc.DESCRIPTOR_ID = item_attr.DESCRIPTOR_ID
AND cat_desc.DELETED_FLAG='N'
INNER JOIN PRD_DWH_VIEW_MTRL.MAT_DESCRIPTOR_V AS attr
ON attr.DESCRIPTOR_ID = item_attr.DESCRIPTOR_ID
AND attr.DELETED_FLAG = 'N'
INNER JOIN PRD_DWH_VIEW_LMT.Prod_Yellow_Heir_Class_View AS yellow
ON yellow.PRODUCT_ID = item.MATERIAL_NO
WHERE item.SALES_STATUS NOT IN ('DG', 'DV', 'WV', 'WG')
AND LOWER({}) LIKE LOWER({})
"""
#get basic SKU list and hierarchy data from Grainger teradata material universe
grainger_basic_query="""
SELECT item.MATERIAL_NO AS Grainger_SKU
, cat.SEGMENT_ID AS Segment_ID
, cat.SEGMENT_NAME AS Segment_Name
, cat.FAMILY_ID AS Family_ID
, cat.FAMILY_NAME AS Family_Name
, cat.CATEGORY_ID AS Category_ID
, cat.CATEGORY_NAME AS Category_Name
, item.PM_CODE
, item.SALES_STATUS
FROM PRD_DWH_VIEW_MTRL.CATEGORY_V AS cat
RIGHT JOIN PRD_DWH_VIEW_LMT.ITEM_V AS item
ON cat.CATEGORY_ID = item.CATEGORY_ID
AND item.DELETED_FLAG = 'N'
AND item.PRODUCT_APPROVED_US_FLAG = 'Y'
AND item.PM_CODE NOT IN ('R9')
WHERE item.SALES_STATUS NOT IN ('DG', 'DV', 'WV', 'WG')
AND {} IN ({})
"""
#variation of the basic query designed to include discontinued items
grainger_discontinued_query="""
SELECT item.MATERIAL_NO AS Grainger_SKU
, cat.SEGMENT_ID AS Segment_ID
, cat.SEGMENT_NAME AS Segment_Name
, cat.FAMILY_ID AS Family_ID
, cat.FAMILY_NAME AS Family_Name
, cat.CATEGORY_ID AS Category_ID
, cat.CATEGORY_NAME AS Category_Name
, item.PM_CODE
, item.SALES_STATUS
FROM PRD_DWH_VIEW_LMT.ITEM_V AS item
INNER JOIN PRD_DWH_VIEW_MTRL.CATEGORY_V AS cat
ON cat.CATEGORY_ID = item.CATEGORY_ID
-- AND item.DELETED_FLAG = 'N'
FULL OUTER JOIN PRD_DWH_VIEW_LMT.Prod_Yellow_Heir_Class_View AS yellow
ON yellow.PRODUCT_ID = item.MATERIAL_NO
FULL OUTER JOIN PRD_DWH_VIEW_LMT.Yellow_Heir_Flattend_view AS flat
ON yellow.PROD_CLASS_ID = flat.Heir_End_Class_Code
WHERE {} IN ({})
"""
gamut_basic_query="""
SELECT
tprod."gtPartNumber" as "Gamut_SKU"
, tprod."supplierSku" as "Grainger_SKU"
, tprod."categoryId" AS "Gamut_Node_ID"
FROM taxonomy_product tprod
WHERE {} IN ({})
"""
gamut_attr_query="""
WITH RECURSIVE tax AS (
SELECT id,
name,
ARRAY[]::INTEGER[] AS ancestors,
ARRAY[]::character varying[] AS ancestor_names
FROM taxonomy_category as category
WHERE "parentId" IS NULL
AND category.deleted = false
UNION ALL
SELECT category.id,
category.name,
tax.ancestors || tax.id,
tax.ancestor_names || tax.name
FROM taxonomy_category as category
INNER JOIN tax ON category."parentId" = tax.id
WHERE category.deleted = false
)
SELECT
array_to_string(tax.ancestor_names || tax.name,' > ') as "Gamut_PIM_Path"
, tax.ancestors[1] as "Gamut_Category_ID"
, tax.ancestor_names[1] as "Gamut_Category_Name"
, tax_att."categoryId" AS "Gamut_Node_ID"
, tax.name as "Gamut_Node_Name"
, tax_att.id as "Gamut_Attr_ID"
, tax_att.name as "Gamut_Attribute_Name"
, tax_att.description as "Gamut_Attribute_Definition"
, tax_att."unitGroupId" as "Unit_Group_ID"
, uom_kind.name as "UOM_Kind"
FROM taxonomy_attribute tax_att
INNER JOIN tax
ON tax.id = tax_att."categoryId"
INNER JOIN unit_group
ON unit_group.id = tax_att."unitGroupId"
FULL OUTER JOIN uom_kind
ON uom_kind.id = unit_group."kindId"
WHERE {} IN ({})
"""
gamut_attr_values="""
WITH RECURSIVE tax AS (
SELECT id,
name,
ARRAY[]::INTEGER[] AS ancestors,
ARRAY[]::character varying[] AS ancestor_names
FROM taxonomy_category as category
WHERE "parentId" IS NULL
AND category.deleted = false
UNION ALL
SELECT category.id,
category.name,
tax.ancestors || tax.id,
tax.ancestor_names || tax.name
FROM taxonomy_category as category
INNER JOIN tax ON category."parentId" = tax.id
WHERE category.deleted = false
)
SELECT
array_to_string(tax.ancestor_names || tax.name,' > ') as "Gamut_PIM_Path"
, tax.ancestors[1] as "Gamut_Category_ID"
, tax.ancestor_names[1] as "Gamut_Category_Name"
, tprod."categoryId" AS "Gamut_Node_ID"
, tax.name as "Gamut_Node_Name"
-- , tprod."gtPartNumber" as "Gamut_SKU"
-- , tprod."supplierSku" as "Grainger_SKU"
, tax_att.id as "Gamut_Attr_ID"
, tax_att.name as "Gamut_Attribute_Name"
, tax_att.description as "Gamut_Attribute_Definition"
-- , tprodvalue.value as "Original Value"
-- , tprodvalue.unit as "Original Unit"
-- , tprodvalue."valueNormalized" as "Normalized Value"
, tprodvalue."unitNormalized" As "Normalized Unit"
-- , tax_att."unitGroupId" as "Unit_Group_ID"
FROM taxonomy_product tprod
INNER JOIN tax
ON tax.id = tprod."categoryId"
-- AND (4458 = ANY(tax.ancestors)) --OR 8215 = ANY(tax.ancestors) OR 7739 = ANY(tax.ancestors)) -- *** ADD TOP LEVEL NODES HERE ***
INNER JOIN taxonomy_attribute tax_att
ON tax_att."categoryId" = tprod."categoryId"
INNER JOIN taxonomy_product_attribute_value tprodvalue
ON tprod.id = tprodvalue."productId"
AND tax_att.id = tprodvalue."attributeId"
WHERE {} IN ({})
"""
#get basic SKU list and hierarchy data from Grainger teradata material universe
gamut_hier_query="""
WITH RECURSIVE tax AS (
SELECT id,
name,
ARRAY[]::INTEGER[] AS ancestors,
ARRAY[]::character varying[] AS ancestor_names
FROM taxonomy_category as category
WHERE "parentId" IS NULL
AND category.deleted = false
UNION ALL
SELECT category.id,
category.name,
tax.ancestors || category."parentId",
tax.ancestor_names || parent_category.name
FROM taxonomy_category as category
JOIN tax on category."parentId" = tax.id
JOIN taxonomy_category parent_category on category."parentId" = parent_category.id
WHERE category.deleted = false
)
SELECT
tprod."gtPartNumber" as "Gamut_SKU"
, tprod."supplierSku" as "Grainger_SKU"
, array_to_string(tax.ancestor_names || tax.name,' > ') as "tax_path"
, tprod."categoryId" as "PIM Node ID"
FROM taxonomy_product tprod
INNER JOIN tax
ON tax.id = tprod."categoryId"
-- AND (10006 = ANY(tax.ancestors)) --OR 8215 = ANY(tax.ancestors) OR 7739 = ANY(tax.ancestors)) -- *** ADD TOP LEVEL NODES HERE ***
WHERE {} IN ({})
"""