Compare commits
4 Commits
f013c5ccc2
...
8b6df1c917
Author | SHA1 | Date | |
---|---|---|---|
8b6df1c917 | |||
18b8883dde | |||
414f830b19 | |||
0bdf7afca2 |
280
new_targets/history.json
Normal file
280
new_targets/history.json
Normal file
@ -0,0 +1,280 @@
|
|||||||
|
{
|
||||||
|
"v1:B..CSE..": [
|
||||||
|
{
|
||||||
|
"version": "Actual",
|
||||||
|
"part": "XNS0T1G2G18C140",
|
||||||
|
"qty": 8960.00,
|
||||||
|
"price": 0.092910,
|
||||||
|
"odate": "2016-02-10",
|
||||||
|
"ordnum": 784718,
|
||||||
|
"quoten": 28971,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Quotes",
|
||||||
|
"part": "XNS0T1G2G18C140",
|
||||||
|
"qty": 9100.00,
|
||||||
|
"price": 0.092910,
|
||||||
|
"odate": "2016-02-10",
|
||||||
|
"ordnum": 0,
|
||||||
|
"quoten": 29543,
|
||||||
|
"flags": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"v1:T..CSE..": [
|
||||||
|
{
|
||||||
|
"version": "Quotes",
|
||||||
|
"part": "XNS0T1G2X03C140",
|
||||||
|
"qty": 6020.00,
|
||||||
|
"price": 0.092910,
|
||||||
|
"odate": "2016-02-10",
|
||||||
|
"ordnum": 0,
|
||||||
|
"quoten": 29543,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Actual",
|
||||||
|
"part": "XNS0T1G2X03C140",
|
||||||
|
"qty": 6020.00,
|
||||||
|
"price": 0.092910,
|
||||||
|
"odate": "2016-02-09",
|
||||||
|
"ordnum": 784655,
|
||||||
|
"flags": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"v1:B..PLT..": [
|
||||||
|
{
|
||||||
|
"version": "Actual",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 9600.00,
|
||||||
|
"price": 0.126130,
|
||||||
|
"odate": "2024-10-31",
|
||||||
|
"ordnum": 992442,
|
||||||
|
"quoten": 79774,
|
||||||
|
"flags": [
|
||||||
|
"most_recent_sale",
|
||||||
|
"largest_volume_sale"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Quotes",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 38400.00,
|
||||||
|
"price": 0.126130,
|
||||||
|
"odate": "2024-10-29",
|
||||||
|
"ordnum": 0,
|
||||||
|
"quoten": 79774,
|
||||||
|
"flags": [
|
||||||
|
"most_recent_quote",
|
||||||
|
"largest_volume_quote"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Actual",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 9600.00,
|
||||||
|
"price": 0.126130,
|
||||||
|
"odate": "2024-08-02",
|
||||||
|
"ordnum": 985899,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Actual",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 38400.00,
|
||||||
|
"price": 0.128250,
|
||||||
|
"odate": "2023-12-04",
|
||||||
|
"ordnum": 969894,
|
||||||
|
"quoten": 75273,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Quotes",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 38400.00,
|
||||||
|
"price": 0.128250,
|
||||||
|
"odate": "2023-12-04",
|
||||||
|
"ordnum": 0,
|
||||||
|
"quoten": 75273,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Quotes",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 19200.00,
|
||||||
|
"price": 0.135000,
|
||||||
|
"odate": "2023-02-13",
|
||||||
|
"ordnum": 0,
|
||||||
|
"quoten": 73602,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Quotes",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 38400.00,
|
||||||
|
"price": 0.145020,
|
||||||
|
"odate": "2022-01-26",
|
||||||
|
"ordnum": 0,
|
||||||
|
"quoten": 69130,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Actual",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 28800.00,
|
||||||
|
"price": 0.125480,
|
||||||
|
"odate": "2021-07-16",
|
||||||
|
"ordnum": 922664,
|
||||||
|
"quoten": 64686,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Quotes",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 38400.00,
|
||||||
|
"price": 0.125480,
|
||||||
|
"odate": "2021-07-08",
|
||||||
|
"ordnum": 0,
|
||||||
|
"quoten": 64686,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Actual",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 38400.00,
|
||||||
|
"price": 0.100390,
|
||||||
|
"odate": "2020-11-12",
|
||||||
|
"ordnum": 905400,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Quotes",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 38400.00,
|
||||||
|
"price": 0.100390,
|
||||||
|
"odate": "2020-08-13",
|
||||||
|
"ordnum": 0,
|
||||||
|
"quoten": 58215,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Quotes",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 28800.00,
|
||||||
|
"price": 0.100390,
|
||||||
|
"odate": "2020-06-11",
|
||||||
|
"ordnum": 0,
|
||||||
|
"quoten": 57254,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Actual",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 28800.00,
|
||||||
|
"price": 0.100390,
|
||||||
|
"odate": "2020-06-10",
|
||||||
|
"ordnum": 890715,
|
||||||
|
"quoten": 57254,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Actual",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 28800.00,
|
||||||
|
"price": 0.098910,
|
||||||
|
"odate": "2019-07-23",
|
||||||
|
"ordnum": 870300,
|
||||||
|
"quoten": 51460,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Quotes",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 28800.00,
|
||||||
|
"price": 0.098910,
|
||||||
|
"odate": "2019-07-18",
|
||||||
|
"ordnum": 0,
|
||||||
|
"quoten": 51460,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Actual",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 19200.00,
|
||||||
|
"price": 0.095290,
|
||||||
|
"odate": "2018-10-09",
|
||||||
|
"ordnum": 853112,
|
||||||
|
"quoten": 44775,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Actual",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 9600.00,
|
||||||
|
"price": 0.095290,
|
||||||
|
"odate": "2018-10-09",
|
||||||
|
"ordnum": 853112,
|
||||||
|
"quoten": 44775,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Quotes",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 9600.00,
|
||||||
|
"price": 0.095290,
|
||||||
|
"odate": "2018-08-21",
|
||||||
|
"ordnum": 0,
|
||||||
|
"quoten": 44775,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Quotes",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 38400.00,
|
||||||
|
"price": 0.094160,
|
||||||
|
"odate": "2017-10-13",
|
||||||
|
"ordnum": 0,
|
||||||
|
"quoten": 38844,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Actual",
|
||||||
|
"part": "XNS0T1G2G18B079",
|
||||||
|
"qty": 47790.00,
|
||||||
|
"price": 0.092910,
|
||||||
|
"odate": "2016-06-10",
|
||||||
|
"ordnum": 794232,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Quotes",
|
||||||
|
"part": "XNS0T1G2G18B079",
|
||||||
|
"qty": 47790.00,
|
||||||
|
"price": 0.092910,
|
||||||
|
"odate": "2016-02-10",
|
||||||
|
"ordnum": 0,
|
||||||
|
"quoten": 29543,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Quotes",
|
||||||
|
"part": "XNS0T1G2G18B079",
|
||||||
|
"qty": 47790.00,
|
||||||
|
"price": 0.091200,
|
||||||
|
"odate": "2015-12-17",
|
||||||
|
"ordnum": 0,
|
||||||
|
"quoten": 28971,
|
||||||
|
"flags": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"version": "Quotes",
|
||||||
|
"part": "XNS0T1G2G18B079",
|
||||||
|
"qty": 47790.00,
|
||||||
|
"price": 0.091200,
|
||||||
|
"odate": "2015-09-21",
|
||||||
|
"ordnum": 0,
|
||||||
|
"quoten": 27873,
|
||||||
|
"flags": []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
63
new_targets/history_new.json
Normal file
63
new_targets/history_new.json
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
{
|
||||||
|
"mrs": {
|
||||||
|
"version": "Actual",
|
||||||
|
"datasegment": "v1:T..CSE..",
|
||||||
|
"part": "XNS0T1G3X18C140",
|
||||||
|
"qty": 4200.00,
|
||||||
|
"price": 0.146300,
|
||||||
|
"odate": "2025-08-01",
|
||||||
|
"ordnum": 1011917,
|
||||||
|
"flag": "mrs"
|
||||||
|
},
|
||||||
|
"mrq": {
|
||||||
|
"version": "Quotes",
|
||||||
|
"datasegment": "v1:B..CSE..",
|
||||||
|
"part": "XNS0T1G3G18C140",
|
||||||
|
"qty": 7560.00,
|
||||||
|
"price": 0.115800,
|
||||||
|
"odate": "2024-12-10",
|
||||||
|
"ordnum": 0,
|
||||||
|
"quoten": 81983,
|
||||||
|
"flag": "mrq"
|
||||||
|
},
|
||||||
|
"lvs": {
|
||||||
|
"version": "Actual",
|
||||||
|
"datasegment": "v1:B..PLT..",
|
||||||
|
"part": "XNS0T1G3G18B096",
|
||||||
|
"qty": 28800.00,
|
||||||
|
"price": 0.098360,
|
||||||
|
"odate": "2025-05-23",
|
||||||
|
"ordnum": 1006860,
|
||||||
|
"flag": "lvs"
|
||||||
|
},
|
||||||
|
"lvq": {
|
||||||
|
"version": "Quotes",
|
||||||
|
"datasegment": "v1:B..CSE..",
|
||||||
|
"part": "XNS0T1G3G18C140",
|
||||||
|
"qty": 7560.00,
|
||||||
|
"price": 0.115800,
|
||||||
|
"odate": "2024-12-10",
|
||||||
|
"ordnum": 0,
|
||||||
|
"quoten": 81983,
|
||||||
|
"flag": "lvq"
|
||||||
|
},
|
||||||
|
"v1:B..CSE..": {
|
||||||
|
"dss": {
|
||||||
|
"version": "Actual",
|
||||||
|
"datasegment": "v1:B..CSE..",
|
||||||
|
"part": "XNS0T1G3G18C140",
|
||||||
|
"qty": 2520.00,
|
||||||
|
"price": 0.106540,
|
||||||
|
"odate": "2025-07-17",
|
||||||
|
"ordnum": 1010952
|
||||||
|
},
|
||||||
|
"dsq": {
|
||||||
|
"version": "Quotes",
|
||||||
|
"datasegment": "v1:B..CSE..",
|
||||||
|
"part": "XNS0T1G3G18C140",
|
||||||
|
"qty": 2520.00,
|
||||||
|
"price": 0.106540,
|
||||||
|
"odate": "2025-07-17",
|
||||||
|
"quoten": 81983
|
||||||
|
}}
|
||||||
|
}
|
@ -29,7 +29,7 @@ srt AS (
|
|||||||
i.item = o.[Part Code]
|
i.item = o.[Part Code]
|
||||||
WHERE
|
WHERE
|
||||||
--quotes can't be integrated until we have datasegment or correct part code
|
--quotes can't be integrated until we have datasegment or correct part code
|
||||||
o.[Data Source] IN ('Actual'/*,'Quotes'*/) AND
|
o.[Data Source] IN ('Actual','Quotes') AND
|
||||||
customer IS NOT NULL AND
|
customer IS NOT NULL AND
|
||||||
[Financial Statement Line] = '41010' AND
|
[Financial Statement Line] = '41010' AND
|
||||||
o.[Order Status] <> 'CANCELLED' AND
|
o.[Order Status] <> 'CANCELLED' AND
|
||||||
@ -66,3 +66,199 @@ INSERT INTO pricing.lastprice SELECT * FROM onerow;
|
|||||||
--SELECT * FROM pricing.lastprice l
|
--SELECT * FROM pricing.lastprice l
|
||||||
|
|
||||||
CREATE UNIQUE INDEX lastprice_cust_partgroup ON pricing.lastprice(customer, partgroup);
|
CREATE UNIQUE INDEX lastprice_cust_partgroup ON pricing.lastprice(customer, partgroup);
|
||||||
|
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
-- Step 1: Rebuild last price history with flags for recency & feature-match
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
-- Step 1: Rebuild last price history with global recency + part-level feature match
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
DELETE FROM pricing.lastprice;
|
||||||
|
|
||||||
|
WITH base AS (
|
||||||
|
SELECT
|
||||||
|
o."Customer" AS customer,
|
||||||
|
o."Part Group" AS partgroup,
|
||||||
|
RTRIM(i.V1DS) AS dataseg,
|
||||||
|
o."Data Source" AS version, -- 'Actual' or 'Quotes'
|
||||||
|
o."Part Code" AS part,
|
||||||
|
o."Units" AS qty,
|
||||||
|
ROUND(o.[Value USD] / o.[Units], 5) AS price,
|
||||||
|
o.[Order Date] AS odate,
|
||||||
|
o.[Order Number] AS ordnum,
|
||||||
|
o.[Quote Number] AS quoten
|
||||||
|
FROM
|
||||||
|
rlarp.osm_stack_pretty o
|
||||||
|
INNER JOIN CMSInterfaceIn.[CMS.CUSLG].ITEMM i
|
||||||
|
ON i.item = o.[Part Code]
|
||||||
|
WHERE
|
||||||
|
o.[Data Source] IN ('Actual', 'Quotes') AND
|
||||||
|
o."Customer" IS NOT NULL AND
|
||||||
|
o."Financial Statement Line" = '41010' AND
|
||||||
|
o."Order Status" <> 'CANCELLED' AND
|
||||||
|
o."Units" <> 0 AND
|
||||||
|
o."Part Group" <> ''
|
||||||
|
),
|
||||||
|
ranked AS (
|
||||||
|
SELECT
|
||||||
|
b.*,
|
||||||
|
ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY b.customer, b.partgroup
|
||||||
|
ORDER BY CASE WHEN b.version = 'Actual' THEN b.odate ELSE NULL END DESC
|
||||||
|
) AS rn_recent_sale,
|
||||||
|
ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY b.customer, b.partgroup
|
||||||
|
ORDER BY CASE WHEN b.version = 'Quotes' THEN b.odate ELSE NULL END DESC
|
||||||
|
) AS rn_recent_quote
|
||||||
|
FROM base b
|
||||||
|
),
|
||||||
|
last_feature_sale AS (
|
||||||
|
SELECT *
|
||||||
|
FROM (
|
||||||
|
SELECT *,
|
||||||
|
ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY customer, partgroup, part
|
||||||
|
ORDER BY odate DESC
|
||||||
|
) AS rn
|
||||||
|
FROM base
|
||||||
|
WHERE version = 'Actual'
|
||||||
|
) x
|
||||||
|
WHERE rn = 1
|
||||||
|
),
|
||||||
|
last_feature_quote AS (
|
||||||
|
SELECT *
|
||||||
|
FROM (
|
||||||
|
SELECT *,
|
||||||
|
ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY customer, partgroup, part
|
||||||
|
ORDER BY odate DESC
|
||||||
|
) AS rn
|
||||||
|
FROM base
|
||||||
|
WHERE version = 'Quotes'
|
||||||
|
) x
|
||||||
|
WHERE rn = 1
|
||||||
|
),
|
||||||
|
flagged AS (
|
||||||
|
SELECT
|
||||||
|
r.*,
|
||||||
|
CASE WHEN rn_recent_sale = 1 THEN 'most_recent_sale' END AS flag1,
|
||||||
|
CASE WHEN rn_recent_quote = 1 THEN 'most_recent_quote' END AS flag2,
|
||||||
|
CASE
|
||||||
|
WHEN r.version = 'Actual'
|
||||||
|
AND r.customer = s.customer
|
||||||
|
AND r.partgroup = s.partgroup
|
||||||
|
AND r.part = s.part
|
||||||
|
AND r.odate = s.odate
|
||||||
|
AND r.ordnum = s.ordnum
|
||||||
|
THEN 'last_feature_match_sale'
|
||||||
|
END AS flag3,
|
||||||
|
CASE
|
||||||
|
WHEN r.version = 'Quotes'
|
||||||
|
AND r.customer = q.customer
|
||||||
|
AND r.partgroup = q.partgroup
|
||||||
|
AND r.part = q.part
|
||||||
|
AND r.odate = q.odate
|
||||||
|
AND r.quoten = q.quoten
|
||||||
|
THEN 'last_feature_match_quote'
|
||||||
|
END AS flag4
|
||||||
|
FROM ranked r
|
||||||
|
LEFT JOIN last_feature_sale s
|
||||||
|
ON r.version = 'Actual'
|
||||||
|
AND r.customer = s.customer
|
||||||
|
AND r.partgroup = s.partgroup
|
||||||
|
AND r.part = s.part
|
||||||
|
LEFT JOIN last_feature_quote q
|
||||||
|
ON r.version = 'Quotes'
|
||||||
|
AND r.customer = q.customer
|
||||||
|
AND r.partgroup = q.partgroup
|
||||||
|
AND r.part = q.part
|
||||||
|
)
|
||||||
|
SELECT * FROM flagged WHERE customer LIKE 'ESBENSHADE%' AND partgroup = 'XNS0T1G3' AND flag3 = 'last_feature_match_sale'
|
||||||
|
|
||||||
|
flags_aggregated AS (
|
||||||
|
SELECT
|
||||||
|
customer,
|
||||||
|
partgroup,
|
||||||
|
dataseg,
|
||||||
|
version,
|
||||||
|
part,
|
||||||
|
qty,
|
||||||
|
price,
|
||||||
|
odate,
|
||||||
|
ordnum,
|
||||||
|
quoten,
|
||||||
|
JSON_QUERY(
|
||||||
|
'[' + STRING_AGG(QUOTENAME(flag, '"'), ',') + ']'
|
||||||
|
) AS flags
|
||||||
|
FROM (
|
||||||
|
SELECT
|
||||||
|
customer, partgroup, dataseg, version, part, qty, price, odate, ordnum, quoten,
|
||||||
|
flag
|
||||||
|
FROM flagged
|
||||||
|
CROSS APPLY (VALUES (flag1), (flag2), (flag3), (flag4)) AS f(flag)
|
||||||
|
WHERE flag IS NOT NULL
|
||||||
|
) AS flags_expanded
|
||||||
|
GROUP BY customer, partgroup, dataseg, version, part, qty, price, odate, ordnum, quoten
|
||||||
|
),
|
||||||
|
all_rows_with_flags AS (
|
||||||
|
SELECT
|
||||||
|
b.customer,
|
||||||
|
b.partgroup,
|
||||||
|
b.dataseg,
|
||||||
|
b.version,
|
||||||
|
b.part,
|
||||||
|
b.qty,
|
||||||
|
b.price,
|
||||||
|
b.odate,
|
||||||
|
b.ordnum,
|
||||||
|
b.quoten,
|
||||||
|
ISNULL(f.flags, JSON_QUERY('[]')) AS flags
|
||||||
|
FROM base b
|
||||||
|
LEFT JOIN flags_aggregated f
|
||||||
|
ON b.customer = f.customer
|
||||||
|
AND b.partgroup = f.partgroup
|
||||||
|
AND b.dataseg = f.dataseg
|
||||||
|
AND b.version = f.version
|
||||||
|
AND b.part = f.part
|
||||||
|
AND b.qty = f.qty
|
||||||
|
AND b.price = f.price
|
||||||
|
AND b.odate = f.odate
|
||||||
|
AND b.ordnum = f.ordnum
|
||||||
|
AND ISNULL(b.quoten, -1) = ISNULL(f.quoten, -1)
|
||||||
|
),
|
||||||
|
json_rows AS (
|
||||||
|
SELECT
|
||||||
|
customer,
|
||||||
|
partgroup,
|
||||||
|
dataseg,
|
||||||
|
CONCAT(
|
||||||
|
'"', dataseg, '":',
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
version, part, qty, price, odate, ordnum, quoten, flags
|
||||||
|
FROM all_rows_with_flags sub
|
||||||
|
WHERE sub.customer = main.customer
|
||||||
|
AND sub.partgroup = main.partgroup
|
||||||
|
AND sub.dataseg = main.dataseg
|
||||||
|
FOR JSON PATH
|
||||||
|
)
|
||||||
|
) AS part_json
|
||||||
|
FROM all_rows_with_flags main
|
||||||
|
GROUP BY customer, partgroup, dataseg
|
||||||
|
),
|
||||||
|
onerow AS (
|
||||||
|
SELECT
|
||||||
|
customer,
|
||||||
|
partgroup,
|
||||||
|
CONCAT('{', STRING_AGG(part_json, ','), '}') AS part_stats
|
||||||
|
FROM json_rows
|
||||||
|
GROUP BY customer, partgroup
|
||||||
|
)
|
||||||
|
|
||||||
|
INSERT INTO pricing.lastprice
|
||||||
|
SELECT * FROM onerow;
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX lastprice_cust_partgroup ON pricing.lastprice(customer, partgroup);
|
||||||
|
221
new_targets/tables/lastpricedetail.ms.sql
Normal file
221
new_targets/tables/lastpricedetail.ms.sql
Normal file
@ -0,0 +1,221 @@
|
|||||||
|
--------------------------------------------------------------------------------
|
||||||
|
-- Reset target tables
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
--DROP TABLE IF EXISTS pricing.lastpricedetail;
|
||||||
|
DELETE FROM pricing.lastpricedetail;
|
||||||
|
DROP TABLE IF EXISTS #flagged;
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
-- Stage 1: Load cleaned input rows
|
||||||
|
-- Filters out irrelevant quotes/orders and calculates unit prices
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
WITH base AS (
|
||||||
|
SELECT
|
||||||
|
o."Customer" AS customer,
|
||||||
|
o."Part Group" AS partgroup,
|
||||||
|
RTRIM(i.V1DS) AS dataseg,
|
||||||
|
o."Data Source" AS version,
|
||||||
|
o."Part Code" AS part,
|
||||||
|
o."Units" AS qty,
|
||||||
|
CASE
|
||||||
|
WHEN o."Units" = 0 THEN NULL
|
||||||
|
ELSE ROUND(o.[Value USD] / NULLIF(o."Units", 0), 5)
|
||||||
|
END AS price,
|
||||||
|
o.[Order Date] AS odate,
|
||||||
|
o.[Order Number] AS ordnum,
|
||||||
|
o.[Quote Number] AS quoten
|
||||||
|
FROM
|
||||||
|
rlarp.osm_stack_pretty o
|
||||||
|
INNER JOIN CMSInterfaceIn.[CMS.CUSLG].ITEMM i
|
||||||
|
ON i.item = o.[Part Code]
|
||||||
|
WHERE
|
||||||
|
o.[Data Source] IN ('Actual', 'Quotes')
|
||||||
|
AND o."Customer" IS NOT NULL
|
||||||
|
AND o."Financial Statement Line" = '41010'
|
||||||
|
AND o."Order Status" <> 'CANCELLED'
|
||||||
|
AND o."Units" > 0
|
||||||
|
AND o."Part Group" <> ''
|
||||||
|
-- Optional filter for testing
|
||||||
|
-- AND o."Customer" = 'ESBENSHADES GREENHOUSE'
|
||||||
|
),
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
-- Stage 2: Rank each row based on recency and volume rules
|
||||||
|
-- Flags include:
|
||||||
|
-- - rn_mrs: most recent sale
|
||||||
|
-- - rn_mrq: most recent quote
|
||||||
|
-- - rn_lvs: largest sale in last year
|
||||||
|
-- - rn_lvq: largest quote in last year
|
||||||
|
-- - rn_dss: most recent sale per dataseg
|
||||||
|
-- - rn_dsq: most recent quote per dataseg
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
ranked AS (
|
||||||
|
SELECT
|
||||||
|
b.*
|
||||||
|
-- Most recent sale
|
||||||
|
,ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY b.customer, b.partgroup
|
||||||
|
ORDER BY CASE WHEN b.version = 'Actual' THEN b.odate ELSE NULL END DESC
|
||||||
|
) AS rn_mrs
|
||||||
|
-- Most recent quote
|
||||||
|
,ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY b.customer, b.partgroup
|
||||||
|
ORDER BY CASE WHEN b.version = 'Quotes' THEN b.odate ELSE NULL END DESC
|
||||||
|
) AS rn_mrq
|
||||||
|
-- Largest volume sale (last 12 months)
|
||||||
|
,ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY b.customer, b.partgroup
|
||||||
|
ORDER BY CASE
|
||||||
|
WHEN b.version = 'Actual' AND b.odate >= DATEADD(YEAR, -1, GETDATE())
|
||||||
|
THEN b.qty ELSE NULL
|
||||||
|
END DESC
|
||||||
|
) AS rn_lvs
|
||||||
|
-- Largest volume quote (last 12 months)
|
||||||
|
,ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY b.customer, b.partgroup
|
||||||
|
ORDER BY CASE
|
||||||
|
WHEN b.version = 'Quotes' AND b.odate >= DATEADD(YEAR, -1, GETDATE())
|
||||||
|
THEN b.qty ELSE NULL
|
||||||
|
END DESC
|
||||||
|
) AS rn_lvq
|
||||||
|
-- Most recent sale per data segment
|
||||||
|
,ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY b.customer, b.partgroup, b.dataseg, b.version
|
||||||
|
ORDER BY CASE WHEN b.version = 'Actual' THEN b.odate ELSE NULL END DESC
|
||||||
|
) AS rn_dss
|
||||||
|
-- Most recent quote per data segment
|
||||||
|
,ROW_NUMBER() OVER (
|
||||||
|
PARTITION BY b.customer, b.partgroup, b.dataseg, b.version
|
||||||
|
ORDER BY CASE WHEN b.version = 'Quotes' THEN b.odate ELSE NULL END DESC
|
||||||
|
) AS rn_dsq
|
||||||
|
FROM base b
|
||||||
|
)
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
-- Stage 2.5: Save only rows that meet any of the above criteria
|
||||||
|
-- and annotate each with global-level flag (mrs, mrq, lvs, lvq)
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
SELECT
|
||||||
|
*,
|
||||||
|
CASE WHEN rn_mrs = 1 THEN 'mrs' END AS f1,
|
||||||
|
CASE WHEN rn_mrq = 1 THEN 'mrq' END AS f2,
|
||||||
|
CASE WHEN rn_lvs = 1 THEN 'lvs' END AS f3,
|
||||||
|
CASE WHEN rn_lvq = 1 THEN 'lvq' END AS f4,
|
||||||
|
CASE WHEN rn_dss = 1 THEN 'dss' END AS f5,
|
||||||
|
CASE WHEN rn_dsq = 1 THEN 'dsq' END AS f6
|
||||||
|
INTO #flagged
|
||||||
|
FROM ranked
|
||||||
|
WHERE
|
||||||
|
rn_mrs = 1
|
||||||
|
OR rn_mrq = 1
|
||||||
|
OR rn_lvs = 1
|
||||||
|
OR rn_lvq = 1
|
||||||
|
OR rn_dss = 1
|
||||||
|
OR rn_dsq = 1;
|
||||||
|
|
||||||
|
CREATE NONCLUSTERED INDEX ix_flagged_lookup
|
||||||
|
ON #flagged(customer, partgroup, dataseg, version, part, qty, price, odate, ordnum, quoten);
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
-- Stage 3: Build JSON from flagged rows
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
-- Step 3.1: Explode all flags from the #flagged table
|
||||||
|
WITH exploded_flags AS (
|
||||||
|
SELECT
|
||||||
|
customer, partgroup, dataseg, version, part, qty, price, odate, ordnum, quoten,
|
||||||
|
flag
|
||||||
|
FROM #flagged
|
||||||
|
CROSS APPLY (VALUES (f1), (f2), (f3), (f4), (f5), (f6)) AS f(flag)
|
||||||
|
WHERE flag IS NOT NULL
|
||||||
|
)
|
||||||
|
--SELECT * FROM exploded_flags
|
||||||
|
-- Step 3.2: Serialize each row into its JSON snippet
|
||||||
|
,serialized_flags AS (
|
||||||
|
SELECT
|
||||||
|
customer,
|
||||||
|
partgroup,
|
||||||
|
dataseg,
|
||||||
|
flag,
|
||||||
|
CONCAT(
|
||||||
|
'"', flag, '":',
|
||||||
|
JSON_QUERY((
|
||||||
|
SELECT
|
||||||
|
version,
|
||||||
|
dataseg AS datasegment,
|
||||||
|
part,
|
||||||
|
qty,
|
||||||
|
price,
|
||||||
|
odate,
|
||||||
|
ordnum,
|
||||||
|
quoten,
|
||||||
|
flag
|
||||||
|
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
|
||||||
|
))
|
||||||
|
) AS json_piece
|
||||||
|
FROM exploded_flags
|
||||||
|
)
|
||||||
|
--SELECT * FROM serialized_flags
|
||||||
|
-- Step 3.3: Collect all global-level flags (mrs, mrq, lvs, lvq)
|
||||||
|
,flag_json AS (
|
||||||
|
SELECT
|
||||||
|
customer,
|
||||||
|
partgroup,
|
||||||
|
STRING_AGG(json_piece, ',') AS json_block
|
||||||
|
FROM serialized_flags
|
||||||
|
WHERE flag IN ('mrs', 'mrq', 'lvs', 'lvq')
|
||||||
|
GROUP BY customer, partgroup
|
||||||
|
)
|
||||||
|
--SELECT * FROM flag_json
|
||||||
|
-- Step 3.4: Nest dss/dsq under each dataseg
|
||||||
|
,seg_pieces AS (
|
||||||
|
SELECT
|
||||||
|
customer,
|
||||||
|
partgroup,
|
||||||
|
dataseg,
|
||||||
|
STRING_AGG(json_piece, ',') AS inner_json
|
||||||
|
FROM serialized_flags
|
||||||
|
WHERE flag IN ('dss', 'dsq')
|
||||||
|
GROUP BY customer, partgroup, dataseg
|
||||||
|
)
|
||||||
|
--SELECT * FROM seg_pieces
|
||||||
|
-- Step 3.5: Wrap the inner_json under dataseg key
|
||||||
|
,wrapped_segs AS (
|
||||||
|
SELECT
|
||||||
|
customer,
|
||||||
|
partgroup,
|
||||||
|
CONCAT(
|
||||||
|
'"', dataseg, '": {', inner_json, '}'
|
||||||
|
) AS json_piece
|
||||||
|
FROM seg_pieces
|
||||||
|
)
|
||||||
|
-- Step 3.6: Aggregate all dataseg entries into one JSON block per customer/partgroup
|
||||||
|
,seg_json AS (
|
||||||
|
SELECT
|
||||||
|
customer,
|
||||||
|
partgroup,
|
||||||
|
STRING_AGG(json_piece, ',') AS json_block
|
||||||
|
FROM wrapped_segs
|
||||||
|
GROUP BY customer, partgroup
|
||||||
|
)
|
||||||
|
--SELECT * FROM seg_json
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
-- Stage 4: Merge flags and segment blocks into a single JSON object
|
||||||
|
-- Write final pricing history to pricing.lastpricedetail
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
INSERT INTO
|
||||||
|
pricing.lastpricedetail
|
||||||
|
SELECT
|
||||||
|
COALESCE(f.customer, s.customer) AS customer,
|
||||||
|
COALESCE(f.partgroup, s.partgroup) AS partgroup,
|
||||||
|
CONCAT(
|
||||||
|
'{',
|
||||||
|
COALESCE(f.json_block, ''),
|
||||||
|
CASE
|
||||||
|
WHEN f.json_block IS NOT NULL AND s.json_block IS NOT NULL THEN ','
|
||||||
|
ELSE ''
|
||||||
|
END,
|
||||||
|
COALESCE(s.json_block, ''),
|
||||||
|
'}'
|
||||||
|
) AS part_stats
|
||||||
|
FROM flag_json f
|
||||||
|
FULL OUTER JOIN seg_json s
|
||||||
|
ON f.customer = s.customer AND f.partgroup = s.partgroup;
|
Loading…
Reference in New Issue
Block a user