-
Notifications
You must be signed in to change notification settings - Fork 0
/
inv_turn
183 lines (158 loc) · 5.98 KB
/
inv_turn
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
/*+ ETLM {
depend:{
add:[
{name:"dl.class"},
{name:"bk.shipping"},
{name:"dl.inv_ow"}
}
} */
/*find group of items using names inside the title*/
with tita AS
(
SELECT
item,
marketplace,
tita.title
FROM
dl.class tita
WHERE
tita.subcategory_description IN ('%abcd%','%abcd%') OR
(
tita.title ILIKE ('%abcd%%') OR tita.title ILIKE ('%abcd%')
OR tita.title ILIKE ('%abcd%') OR tita.title ILIKE ('%%abcd%')
OR tita.title ILIKE ('%abcd%') OR tita.title ILIKE ('%abcd%')
)
AND tita.title NOT ILIKE ('%abcd%')
AND tita.title NOT ILIKE ('%abcd%')
AND marketplace IN ('DE','UK','FR','IT','ES')
GROUP BY 1,2,3
),
/*returning sales for those items*/
ship AS
(
SELECT
dcsi.item,
dcsi.warehouse,
DATE_PART(week, DATEADD(day,1,dcsi.sales_date)) AS week,
SUM(sales) AS sales
FROM bk.shipping dcsi
WHERE
dcsi.marketplace IN ('DE','UK','FR','IT','ES')
/*return historical data*/
AND DATE_PART(w, dcsi.sales_date) IN
(
(DATE_PART(week,TO_DATE(getdate(), 'YYYY/MM/DD')-7 )),
(DATE_PART(week,TO_DATE(getdate(), 'YYYY/MM/DD')-14)),
(DATE_PART(week,TO_DATE(getdate(), 'YYYY/MM/DD')-21)),
(DATE_PART(week,TO_DATE(getdate(), 'YYYY/MM/DD')-28)),
(DATE_PART(week,TO_DATE(getdate(), 'YYYY/MM/DD')-35)),
(DATE_PART(week,TO_DATE(getdate(), 'YYYY/MM/DD')-42)),
(DATE_PART(week,TO_DATE(getdate(), 'YYYY/MM/DD')-49)),
(DATE_PART(week,TO_DATE(getdate(), 'YYYY/MM/DD')-56)),
(DATE_PART(week,TO_DATE(getdate(), 'YYYY/MM/DD')-63)),
(DATE_PART(week,TO_DATE(getdate(), 'YYYY/MM/DD')-70)),
(DATE_PART(week,TO_DATE(getdate(), 'YYYY/MM/DD')-77)),
(DATE_PART(week,TO_DATE(getdate(), 'YYYY/MM/DD')-84))
)
AND sales_date > getdate()-100 /* to discard last year data*/
AND DCSI.item IN (SELECT item FROM tita)
GROUP BY 1,2,3
),
/*returning inventory and volume for the items*/
fin AS
(
SELECT
DATE_PART(week,ibow.snapshot_day) AS week,
ibow.item,
ibow.warehouse,
tita.marketplace,
ibow.inventory_condition_code,
SUM(ibow.on_hand_quantity) AS units,
tita.title,
CSO.cube_cuft
FROM dl.inv_ow ibow
LEFT JOIN
dl.sort CSO ON ibow.item = CSO.item
AND
CASE
WHEN ibow.warehouse = 'DE78' THEN CSO.marketplace = 'DE'
WHEN ibow.warehouse = 'XD14' THEN CSO.marketplace = 'DE'
WHEN ibow.warehouse = 'XD72' THEN CSO.marketplace = 'DE'
WHEN ibow.warehouse = 'XBER' THEN CSO.marketplace = 'DE'
WHEN ibow.warehouse = 'DE21' THEN CSO.marketplace = 'DE'
WHEN ibow.warehouse = 'XG32' THEN CSO.marketplace = 'UK'
WHEN ibow.warehouse = 'UKSA' THEN CSO.marketplace = 'UK'
WHEN ibow.warehouse = 'RE32' THEN CSO.marketplace = 'UK'
WHEN ibow.warehouse = 'LT62' THEN CSO.marketplace = 'UK'
WHEN ibow.warehouse = 'DFR1' THEN CSO.marketplace = 'FR'
WHEN ibow.warehouse = 'DFR5' THEN CSO.marketplace = 'FR'
WHEN ibow.warehouse = 'RE34' THEN CSO.marketplace = 'IT'
WHEN ibow.warehouse = 'SW78' THEN CSO.marketplace = 'IT'
WHEN ibow.warehouse = 'AS21' THEN CSO.marketplace = 'ES'
WHEN ibow.warehouse = 'BCN3' THEN CSO.marketplace = 'ES'
WHEN ibow.warehouse = 'SA54' THEN CSO.marketplace = 'ES'
END
JOIN
tita ON ibow.item = tita.item
AND
CASE
WHEN ibow.warehouse = 'DE78' THEN tita.marketplace = 'DE'
WHEN ibow.warehouse = 'XD14' THEN tita.marketplace = 'DE'
WHEN ibow.warehouse = 'XD72' THEN tita.marketplace = 'DE'
WHEN ibow.warehouse = 'XBER' THEN tita.marketplace = 'DE'
WHEN ibow.warehouse = 'DE21' THEN tita.marketplace = 'DE'
WHEN ibow.warehouse = 'XG32' THEN tita.marketplace = 'UK'
WHEN ibow.warehouse = 'UKSA' THEN tita.marketplace = 'UK'
WHEN ibow.warehouse = 'RE32' THEN tita.marketplace = 'UK'
WHEN ibow.warehouse = 'LT62' THEN tita.marketplace = 'UK'
WHEN ibow.warehouse = 'DFR1' THEN tita.marketplace = 'FR'
WHEN ibow.warehouse = 'DFR5' THEN tita.marketplace = 'FR'
WHEN ibow.warehouse = 'RE34' THEN tita.marketplace = 'IT'
WHEN ibow.warehouse = 'SW78' THEN tita.marketplace = 'IT'
WHEN ibow.warehouse = 'AS21' THEN tita.marketplace = 'ES'
WHEN ibow.warehouse = 'BCN3' THEN tita.marketplace = 'ES'
WHEN ibow.warehouse = 'SA54' THEN tita.marketplace = 'ES'
END
WHERE ibow.region_id = 2
AND ibow.snapshot_day BETWEEN getdate()-70 AND GETDATE()
AND ibow.warehouse IN ('DE78','XD72','XD14','DE21','XBER','XG32','LT62','UKSA','RE32','DFR1','DFR5','RE34','SW78','AS21','BCN3','SA54')
AND ibow.inventory_condition_code = 'SELLABLE'
AND ibow.on_hand_quantity > 0
AND tita.title NOT ILIKE ('%abcd%')
AND tita.title NOT ILIKE ('%abcd%')
AND DATE_PART(dow, ibow.snapshot_day) = 6
GROUP BY 1,2,3,4,5,7,8
)
SELECT
fin.week,
fin.item,
fin.warehouse,
fin.marketplace,
fin.inventory_condition_code,
fin.units,
fin.title,
fin.cube_cuft,
ship.sales,
ship.sales * 52 / fin.units AS inv_turn
FROM fin
LEFT JOIN ship
ON fin.item = ship.item AND fin.week = ship.week AND fin.warehouse = ship.warehouse
AND
CASE
WHEN ship.warehouse = 'DE78' THEN fin.marketplace = 'DE'
WHEN ship.warehouse = 'XD14' THEN fin.marketplace = 'DE'
WHEN ship.warehouse = 'XD72' THEN fin.marketplace = 'DE'
WHEN ship.warehouse = 'XBER' THEN fin.marketplace = 'DE'
WHEN ship.warehouse = 'DE21' THEN fin.marketplace = 'DE'
WHEN ship.warehouse = 'XG32' THEN fin.marketplace = 'UK'
WHEN ship.warehouse = 'UKSA' THEN fin.marketplace = 'UK'
WHEN ship.warehouse = 'RE32' THEN fin.marketplace = 'UK'
WHEN ship.warehouse = 'LT62' THEN fin.marketplace = 'UK'
WHEN ship.warehouse = 'DFR1' THEN fin.marketplace = 'FR'
WHEN ship.warehouse = 'DFR5' THEN fin.marketplace = 'FR'
WHEN ship.warehouse = 'RE34' THEN fin.marketplace = 'IT'
WHEN ship.warehouse = 'SW78' THEN fin.marketplace = 'IT'
WHEN ship.warehouse = 'AS21' THEN fin.marketplace = 'ES'
WHEN ship.warehouse = 'BCN3' THEN fin.marketplace = 'ES'
WHEN ship.warehouse = 'SA54' THEN fin.marketplace = 'ES'
end