-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1. Current Inventory.sql
63 lines (54 loc) · 1.61 KB
/
1. Current Inventory.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
--Pulls current inventory position. Allocated sku's are included.
--No updates needed if weekly_frcst_stage is updated
DROP TABLE IF EXISTS [current_inventory];
CREATE TABLE [current_inventory] (
[short_partnumber] VARCHAR (54) PRIMARY KEY
,[qty_oh] NUMERIC (10, 2)
) ;
WITH [cte_fill_rate_projections]
(
[idnumber]
,[partnumber]
,[short_partnumber]
,[qty_oh]
)
AS (
SELECT
[SQLAccess.Part].[idnumber]
,[PartNumber]
--a 3 character line code precedes the part number. The line below removes the line code
,RIGHT(
[PartNumber]
,LEN([PartNumber]) -3
) AS [short_part_number]
,SUM( [OnHand] ) AS [qty_oh]
FROM [SQLAccess.Part]
LEFT JOIN [SQLAccess.Partco]
ON [SQLAccess.Part].[idnumber] = [SQLAccess.Partco].[PartId]
WHERE [SQLAccess.Partco].[co] = '2'
GROUP BY
RIGHT(
[SQLAccess.Part].[idnumber]
,LEN([PartNumber]) -3
)
,[PartNumber]
,[SQLAccess.Part].[idnumber]
)
,
[cte_clean_short_partnumber] ( [short_partnumber] )
AS (
SELECT DISTINCT [short_partnumber]
FROM [cte_fill_rate_projections]
JOIN [weekly_frcst_stage]
ON [cte_fill_rate_projections].[short_partnumber] = [weekly_frcst_stage].[item_id]
)
INSERT INTO [current_inventory] (
[short_partnumber]
,[qty_oh]
)
SELECT DISTINCT [cte_clean_short_partnumber].[short_partnumber]
,SUM([cte_fill_rate_projections].[qty_oh])
FROM [cte_fill_rate_projections]
JOIN [cte_clean_short_partnumber]
ON [cte_fill_rate_projections].[short_partnumber] = [cte_clean_short_partnumber].[short_partnumber]
GROUP BY [cte_clean_short_partnumber].[short_partnumber] ;