forecast_api/offline/build_part.sql

52 lines
1.5 KiB
MySQL
Raw Normal View History

2020-02-19 17:05:27 -05:00
WITH
pool AS (
SELECT
substr(a.v6part,1,3) "family"
,substr(a.v6part,1,8) mold
,a.v6part part
,COALESCE(m.avmajg,p.awmajg) majg
,COALESCE(m.avming,p.awming) ming
,COALESCE(m.avmajs,p.awmajs) majs
,COALESCE(m.avmins,p.awmins) mins
FROM
lgdat.stka a
LEFT OUTER JOIN lgdat.stkmm m ON
m.avpart = v6part
LEFT OUTER JOIN lgdat.stkmp p ON
p.awpart = a.v6part
WHERE
COALESCE(awgled, avgled) = '1RE'
AND v6stat = 'A'
--and avmajg = '910'
GROUP BY
a.v6part
,COALESCE(m.avmajg,p.awmajg)
,COALESCE(m.avming,p.awming)
,COALESCE(m.avmajs,p.awmajs)
,COALESCE(m.avmins,p.awmins)
)
,llist AS (
SELECT
family
,mold
,min(part) part
,jsonb_agg(majg) majg
,jsonb_agg(ming) ming
,jsonb_agg(majs) majs
,jsonb_agg(mins) mins
FROM
pool
GROUP BY
family
,mold
)
SELECT
family
,mold
,part
,(SELECT jsonb_agg(x.e) FROM (SELECT DISTINCT a.e FROM jsonb_array_elements(llist.majg) a(e)) x) majg
,(SELECT jsonb_agg(x.e) FROM (SELECT DISTINCT a.e FROM jsonb_array_elements(llist.ming) a(e)) x) ming
,(SELECT jsonb_agg(x.e) FROM (SELECT DISTINCT a.e FROM jsonb_array_elements(llist.majs) a(e)) x) majs
,(SELECT jsonb_agg(x.e) FROM (SELECT DISTINCT a.e FROM jsonb_array_elements(llist.mins) a(e)) x) mins
FROM
llist