commit: 2025-07-28 15:46:52
This commit is contained in:
parent
404f8a4b1e
commit
f28d26dfb0
2
.gitignore
vendored
2
.gitignore
vendored
@ -5,8 +5,6 @@
|
||||
.idea/workspace.xml
|
||||
.project
|
||||
.dbeaver-data-sources.xml
|
||||
Scripts/Script.sql
|
||||
Scripts/
|
||||
Diagrams/
|
||||
.vs/
|
||||
*.swp
|
||||
|
40
Scripts/Script-1.sql
Normal file
40
Scripts/Script-1.sql
Normal file
@ -0,0 +1,40 @@
|
||||
SELECT
|
||||
o.qline,
|
||||
o.part,
|
||||
o.touched,
|
||||
o.qcustomer,
|
||||
lp.customer,
|
||||
lp.mold,
|
||||
p.[key] AS part, -- this is the part number
|
||||
j.qty,
|
||||
j.price,
|
||||
j.odate,
|
||||
j.ordnum,
|
||||
j.quoten
|
||||
FROM
|
||||
rlarp.live_quotes o
|
||||
LEFT OUTER JOIN pricing.lastprice lp ON
|
||||
lp.customer = o.qcustomer
|
||||
AND lp.mold = substring(o.part,1,8)
|
||||
OUTER APPLY OPENJSON(lp.part_stats) AS p -- unpacks part keys
|
||||
OUTER APPLY OPENJSON(p.value)
|
||||
WITH (
|
||||
qty FLOAT,
|
||||
price FLOAT,
|
||||
odate DATE,
|
||||
ordnum INT,
|
||||
quoten INT
|
||||
) AS j
|
||||
WHERE
|
||||
qid = 112859
|
||||
AND o.part = p.[key] COLLATE SQL_Latin1_General_CP1_CI_AS
|
||||
|
||||
|
||||
SELECT
|
||||
o.qline,
|
||||
o.part,
|
||||
o.touched
|
||||
FROM
|
||||
rlarp.live_quotes o
|
||||
WHERE
|
||||
o.qid = 112794
|
24
Scripts/Script-2.sql
Normal file
24
Scripts/Script-2.sql
Normal file
@ -0,0 +1,24 @@
|
||||
SELECT * FROM pricing.lastprice WHERE customer = 'ALTMAN PLANTS' AND mold = 'XPR15CS1'
|
||||
|
||||
|
||||
SELECT
|
||||
lp.*
|
||||
,p.*
|
||||
,j.*
|
||||
FROM
|
||||
pricing.lastprice lp
|
||||
OUTER APPLY OPENJSON(lp.part_stats) AS p -- unpacks part keys
|
||||
OUTER APPLY OPENJSON(p.value)
|
||||
WITH (
|
||||
qty FLOAT,
|
||||
price FLOAT,
|
||||
odate DATE,
|
||||
ordnum INT,
|
||||
quoten INT
|
||||
) AS j
|
||||
WHERE
|
||||
customer = 'ALTMAN PLANTS'
|
||||
AND mold = 'XPR15CS1'
|
||||
|
||||
|
||||
SELECT * FROM pricing.price_queue pq
|
0
Scripts/Script.sql
Normal file
0
Scripts/Script.sql
Normal file
59
new_targets/scripts/make_hist.ms.sql
Normal file
59
new_targets/scripts/make_hist.ms.sql
Normal file
@ -0,0 +1,59 @@
|
||||
--------------------------------------------------------------------------------
|
||||
-- Step 1: Rebuild last price history at sales matrix refresh time
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
DELETE FROM pricing.lastprice;
|
||||
|
||||
WITH srt AS (
|
||||
SELECT
|
||||
customer,
|
||||
mold,
|
||||
part,
|
||||
version,
|
||||
qty,
|
||||
ROUND(sales_usd / qty, 5) AS price,
|
||||
odate,
|
||||
oseas,
|
||||
ordnum,
|
||||
quoten,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY customer, mold, part, version
|
||||
ORDER BY odate DESC
|
||||
) AS rn
|
||||
FROM rlarp.osm_stack
|
||||
WHERE
|
||||
--quotes can't be integrated until we have datasegment or correct part code
|
||||
version IN ('Actual'/*,'Quotes'*/) AND
|
||||
customer IS NOT NULL AND
|
||||
fs_line = '41010' AND
|
||||
calc_status <> 'CANCELLED' AND
|
||||
qty <> 0 AND
|
||||
mold <> ''
|
||||
),
|
||||
json_rows AS (
|
||||
SELECT
|
||||
customer,
|
||||
mold,
|
||||
part,
|
||||
version,
|
||||
CONCAT(
|
||||
'"', part, '":',
|
||||
(
|
||||
SELECT version, qty, price, odate, ordnum, quoten
|
||||
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
|
||||
)
|
||||
) AS part_json
|
||||
FROM srt
|
||||
WHERE rn = 1
|
||||
)
|
||||
,onerow AS (
|
||||
SELECT
|
||||
customer,
|
||||
mold,
|
||||
CONCAT('{', STRING_AGG(part_json, ','), '}') AS part_stats
|
||||
FROM json_rows
|
||||
GROUP BY customer, mold
|
||||
)
|
||||
INSERT INTO pricing.lastprice SELECT * FROM onerow;
|
||||
|
||||
--CREATE UNIQUE INDEX lastprice_cust_mold ON pricing.lastprice(customer, mold);
|
50
new_targets/scripts/make_hist.pg.sql
Normal file
50
new_targets/scripts/make_hist.pg.sql
Normal file
@ -0,0 +1,50 @@
|
||||
CREATE TABLE pricequote.lastprice AS (
|
||||
WITH
|
||||
--------SORT--------
|
||||
srt AS (
|
||||
SELECT
|
||||
customer
|
||||
,partgroup
|
||||
,dataseg
|
||||
,qtyord
|
||||
,ROUND(sales_usd/qty,5) price
|
||||
,odate
|
||||
,oseas
|
||||
,ordnum
|
||||
,quoten
|
||||
,row_number() OVER (PARTITION BY customer, partgroup, dataseg, version ORDER BY odate DESC) seq
|
||||
,version
|
||||
FROM
|
||||
rlarp.osm_stack
|
||||
WHERE
|
||||
version IN ('Actual','Quotes')
|
||||
AND customer IS NOT NULL
|
||||
AND fs_line = '41010'
|
||||
AND calc_status <> 'CANCELLED'
|
||||
-- AND customer = 'ALTMAN PLANTS'
|
||||
AND qty <> 0
|
||||
AND partgroup <> ''
|
||||
AND version = 'Actual'
|
||||
-- LIMIT 10000
|
||||
)
|
||||
,onerow AS (
|
||||
SELECT
|
||||
customer,
|
||||
partgroup,
|
||||
-- Latest per-dataseg sales wrapped as JSONB object
|
||||
jsonb_object_agg(
|
||||
dataseg,
|
||||
to_jsonb(srt)
|
||||
ORDER BY odate DESC
|
||||
) AS dataseg_stats
|
||||
FROM
|
||||
srt
|
||||
WHERE
|
||||
seq = 1
|
||||
-- AND customer = 'ALTMAN PLANTS'
|
||||
-- AND partgroup ~ 'XPR15CS'
|
||||
GROUP BY customer, partgroup
|
||||
-- ORDER BY customer, partgroup
|
||||
)
|
||||
SELECT * FROM onerow --WHERE customer = 'ALTMAN PLANTS' AND partgroup = 'XPR15CS1'
|
||||
) WITH DATA;
|
90
new_targets/scripts/matrix_apply.ms.sql
Normal file
90
new_targets/scripts/matrix_apply.ms.sql
Normal file
@ -0,0 +1,90 @@
|
||||
|
||||
-- SELECT count(*) FROM rlarp.osm_stack o INNER JOIN #lastprice l ON
|
||||
-- l.customer = o.customer AND o.mold = l.mold
|
||||
|
||||
SELECT
|
||||
o.ordnum,
|
||||
o.part,
|
||||
o.odate,
|
||||
lp.customer,
|
||||
lp.mold,
|
||||
p.[key] AS part, -- this is the part number
|
||||
j.qty,
|
||||
j.price,
|
||||
j.odate,
|
||||
j.ordnum,
|
||||
j.quoten
|
||||
FROM
|
||||
rlarp.osm_stack o
|
||||
LEFT OUTER JOIN pricing.lastprice lp ON
|
||||
lp.customer = o.customer
|
||||
AND lp.mold = o.mold
|
||||
CROSS APPLY OPENJSON(lp.part_stats) AS p -- unpacks part keys
|
||||
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
|
||||
|
||||
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