From f013c5ccc2a5ceff0178f9d208b217c2e67144b4 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Tue, 5 Aug 2025 20:53:14 -0400 Subject: [PATCH] convert price history to work off part group and data segment --- new_targets/procs/single_price_call.ms.sql | 9 ++-- new_targets/scripts/make_hist.ms.sql | 55 ++++++++++++---------- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/new_targets/procs/single_price_call.ms.sql b/new_targets/procs/single_price_call.ms.sql index 48a6289..35a7844 100644 --- a/new_targets/procs/single_price_call.ms.sql +++ b/new_targets/procs/single_price_call.ms.sql @@ -14,6 +14,7 @@ BEGIN ship VARCHAR(100), part VARCHAR(100), stlc VARCHAR(100), + partgroup VARCHAR(100), v1ds VARCHAR(100), vol NUMERIC(18,6), chan VARCHAR(50), @@ -80,7 +81,8 @@ BEGIN END ELSE bc.plevel END, - stlc = substring(q.part,1,8) + stlc = substring(q.part,1,8), + partgroup = i.partgroup FROM @queue q LEFT JOIN rlarp.cust bc ON bc.code = q.bill LEFT JOIN rlarp.cust sc ON sc.code = q.ship @@ -143,19 +145,20 @@ BEGIN FROM @queue q JOIN pricing.lastprice lp ON lp.customer = q.cust - AND lp.mold = SUBSTRING(q.part, 1, 8) + AND lp.partgroup = q.partgroup OUTER APPLY ( SELECT TOP 1 * FROM OPENJSON(lp.part_stats) AS p OUTER APPLY OPENJSON(p.value) WITH ( + part VARCHAR(100), qty NUMERIC(20,5), price NUMERIC(20,5), odate DATE, ordnum INT, quoten INT ) AS j - WHERE p.[key] COLLATE SQL_Latin1_General_CP1_CI_AS = q.part + WHERE p.[key] COLLATE SQL_Latin1_General_CP1_CI_AS = q.v1ds ORDER BY j.odate DESC ) AS j; diff --git a/new_targets/scripts/make_hist.ms.sql b/new_targets/scripts/make_hist.ms.sql index ec5afa7..493d5cf 100644 --- a/new_targets/scripts/make_hist.ms.sql +++ b/new_targets/scripts/make_hist.ms.sql @@ -3,47 +3,49 @@ -------------------------------------------------------------------------------- DELETE FROM pricing.lastprice; + WITH --------SORT-------- srt AS ( SELECT - "Customer" customer, - "Part Group" partgroup, - i.v1ds dataseg, - "Data Source" version, - "Units" qty, - ROUND(sales_usd / qty, 5) AS price, - odate, - oseas, - ordnum, - quoten, + o."Customer" customer, + o."Part Group" partgroup, + RTRIM(i.V1DS) dataseg, + o."Data Source" version, + o."Part Code" part, + o."Units" qty, + ROUND(o.[Value USD] / o.[Units], 5) AS price, + o.[Order Date] odate, + o.[Order Season] oseas, + o.[Order Number] ordnum, + o.[Quote Number] quoten, ROW_NUMBER() OVER ( - PARTITION BY customer, mold, part, version - ORDER BY odate DESC + PARTITION BY o.Customer , o.[Part Group] , i.V1DS, o.[Data Source] + ORDER BY o."Order Date" DESC ) AS rn FROM rlarp.osm_stack_pretty o INNER JOIN CMSInterfaceIn.[CMS.CUSLG].ITEMM i ON - i.item = o.part + i.item = o.[Part Code] WHERE --quotes can't be integrated until we have datasegment or correct part code - version IN ('Actual'/*,'Quotes'*/) AND + o.[Data Source] IN ('Actual'/*,'Quotes'*/) AND customer IS NOT NULL AND - fs_line = '41010' AND - calc_status <> 'CANCELLED' AND - qty <> 0 AND - mold <> '' + [Financial Statement Line] = '41010' AND + o.[Order Status] <> 'CANCELLED' AND + o.Units <> 0 AND + o.[Part Group] <> '' ), json_rows AS ( SELECT customer, - mold, - part, + partgroup, + dataseg, version, CONCAT( - '"', part, '":', + '"', dataseg, '":', ( - SELECT version, qty, price, odate, ordnum, quoten + SELECT version, part, qty, price, odate, ordnum, quoten FOR JSON PATH, WITHOUT_ARRAY_WRAPPER ) ) AS part_json @@ -53,11 +55,14 @@ json_rows AS ( ,onerow AS ( SELECT customer, - mold, + partgroup, CONCAT('{', STRING_AGG(part_json, ','), '}') AS part_stats FROM json_rows -GROUP BY customer, mold +GROUP BY customer, partgroup ) +--SELECT * INTO pricing.lastprice FROM onerow; INSERT INTO pricing.lastprice SELECT * FROM onerow; ---CREATE UNIQUE INDEX lastprice_cust_mold ON pricing.lastprice(customer, mold); +--SELECT * FROM pricing.lastprice l + +CREATE UNIQUE INDEX lastprice_cust_partgroup ON pricing.lastprice(customer, partgroup);