commit: 2025-07-27 23:34:30
This commit is contained in:
parent
a4f9a43195
commit
5fad9bc777
@ -45,7 +45,9 @@ SELECT
|
|||||||
FROM json_rows
|
FROM json_rows
|
||||||
GROUP BY customer, mold
|
GROUP BY customer, mold
|
||||||
)
|
)
|
||||||
SELECT * INTO #lastprice FROM onerow
|
SELECT * INTO pricing.lastprice FROM onerow
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX lastprice_cust_mold ON pricing.lastprice(customer, mold);
|
||||||
|
|
||||||
SELECT count(*) FROM rlarp.osm_stack o INNER JOIN #lastprice l ON
|
SELECT count(*) FROM rlarp.osm_stack o INNER JOIN #lastprice l ON
|
||||||
l.customer = o.customer AND o.mold = l.mold
|
l.customer = o.customer AND o.mold = l.mold
|
||||||
@ -66,7 +68,7 @@ SELECT
|
|||||||
j.quoten
|
j.quoten
|
||||||
FROM
|
FROM
|
||||||
rlarp.osm_stack o
|
rlarp.osm_stack o
|
||||||
LEFT OUTER JOIN #lastprice lp ON
|
LEFT OUTER JOIN pricing.lastprice lp ON
|
||||||
lp.customer = o.customer
|
lp.customer = o.customer
|
||||||
AND lp.mold = o.mold
|
AND lp.mold = o.mold
|
||||||
CROSS APPLY OPENJSON(lp.part_stats) AS p -- unpacks part keys
|
CROSS APPLY OPENJSON(lp.part_stats) AS p -- unpacks part keys
|
||||||
@ -81,3 +83,60 @@ FROM
|
|||||||
WHERE
|
WHERE
|
||||||
o.customer = 'ALTMAN PLANTS'
|
o.customer = 'ALTMAN PLANTS'
|
||||||
AND o.mold = 'XPR15CS1'
|
AND o.mold = 'XPR15CS1'
|
||||||
|
AND o.ordnum = 935360
|
||||||
|
|
||||||
|
WITH exploded AS (
|
||||||
|
SELECT
|
||||||
|
lp.customer,
|
||||||
|
lp.mold,
|
||||||
|
p.[key] AS part_key,
|
||||||
|
j.qty,
|
||||||
|
j.price,
|
||||||
|
j.odate,
|
||||||
|
j.ordnum,
|
||||||
|
j.quoten,
|
||||||
|
CASE WHEN p.[key] = o.part COLLATE Latin1_General_BIN2 THEN 1 ELSE 0 END AS is_exact_match,
|
||||||
|
ROW_NUMBER() OVER (PARTITION BY lp.customer, lp.mold ORDER BY j.odate DESC) AS rn_most_recent
|
||||||
|
FROM rlarp.osm_stack o
|
||||||
|
LEFT JOIN pricing.lastprice lp ON lp.customer = o.customer AND lp.mold = o.mold
|
||||||
|
CROSS APPLY OPENJSON(lp.part_stats) AS p
|
||||||
|
CROSS APPLY OPENJSON(p.value)
|
||||||
|
WITH (
|
||||||
|
qty FLOAT,
|
||||||
|
price FLOAT,
|
||||||
|
odate DATE,
|
||||||
|
ordnum INT,
|
||||||
|
quoten INT
|
||||||
|
) AS j
|
||||||
|
WHERE
|
||||||
|
o.customer = 'ALTMAN PLANTS'
|
||||||
|
AND o.mold = 'XPR15CS1'
|
||||||
|
AND o.ordnum = 935360
|
||||||
|
),
|
||||||
|
tagged AS (
|
||||||
|
SELECT
|
||||||
|
part_key,
|
||||||
|
qty,
|
||||||
|
price,
|
||||||
|
odate,
|
||||||
|
ordnum,
|
||||||
|
quoten,
|
||||||
|
IIF(is_exact_match = 1, 1, NULL) AS is_exact_match,
|
||||||
|
IIF(rn_most_recent = 1, 1, NULL) AS is_most_recent
|
||||||
|
FROM exploded
|
||||||
|
)
|
||||||
|
--SELECT * FROM taggeg
|
||||||
|
SELECT (
|
||||||
|
SELECT
|
||||||
|
part_key AS [key],
|
||||||
|
qty,
|
||||||
|
price,
|
||||||
|
odate,
|
||||||
|
ordnum,
|
||||||
|
quoten,
|
||||||
|
is_exact_match,
|
||||||
|
is_most_recent
|
||||||
|
FROM tagged
|
||||||
|
FOR JSON PATH, INCLUDE_NULL_VALUES
|
||||||
|
) AS updated_json_array;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user