From f28d26dfb026e416093168e3baf959a52c12c99a Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Mon, 28 Jul 2025 15:46:52 -0400 Subject: [PATCH] commit: 2025-07-28 15:46:52 --- .gitignore | 2 - Scripts/Script-1.sql | 40 +++++++++++ Scripts/Script-2.sql | 24 +++++++ Scripts/Script.sql | 0 new_targets/scripts/make_hist.ms.sql | 59 ++++++++++++++++ new_targets/scripts/make_hist.pg.sql | 50 ++++++++++++++ new_targets/scripts/matrix_apply.ms.sql | 90 +++++++++++++++++++++++++ 7 files changed, 263 insertions(+), 2 deletions(-) create mode 100644 Scripts/Script-1.sql create mode 100644 Scripts/Script-2.sql create mode 100644 Scripts/Script.sql create mode 100644 new_targets/scripts/make_hist.ms.sql create mode 100644 new_targets/scripts/make_hist.pg.sql create mode 100644 new_targets/scripts/matrix_apply.ms.sql diff --git a/.gitignore b/.gitignore index 181b0a4..88d5b4e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,8 +5,6 @@ .idea/workspace.xml .project .dbeaver-data-sources.xml -Scripts/Script.sql -Scripts/ Diagrams/ .vs/ *.swp diff --git a/Scripts/Script-1.sql b/Scripts/Script-1.sql new file mode 100644 index 0000000..88f7540 --- /dev/null +++ b/Scripts/Script-1.sql @@ -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 \ No newline at end of file diff --git a/Scripts/Script-2.sql b/Scripts/Script-2.sql new file mode 100644 index 0000000..d9f2fa4 --- /dev/null +++ b/Scripts/Script-2.sql @@ -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 \ No newline at end of file diff --git a/Scripts/Script.sql b/Scripts/Script.sql new file mode 100644 index 0000000..e69de29 diff --git a/new_targets/scripts/make_hist.ms.sql b/new_targets/scripts/make_hist.ms.sql new file mode 100644 index 0000000..ad930a9 --- /dev/null +++ b/new_targets/scripts/make_hist.ms.sql @@ -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); diff --git a/new_targets/scripts/make_hist.pg.sql b/new_targets/scripts/make_hist.pg.sql new file mode 100644 index 0000000..14bd136 --- /dev/null +++ b/new_targets/scripts/make_hist.pg.sql @@ -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; diff --git a/new_targets/scripts/matrix_apply.ms.sql b/new_targets/scripts/matrix_apply.ms.sql new file mode 100644 index 0000000..4436227 --- /dev/null +++ b/new_targets/scripts/matrix_apply.ms.sql @@ -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; +