price_api/new_targets/make_hist.pg.sql

51 lines
1.1 KiB
SQL

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;