add column number to facilitate pivoting

This commit is contained in:
Paul Trowbridge 2021-01-11 11:05:44 -05:00
parent 85e288cf11
commit 412f0ec082

View File

@ -88,6 +88,7 @@ GROUP BY
,pc.branding
,pc.accs
,pc.suffix
,1 col
,pc.pckg1 pckg
,pc.vol1_uom vol_uom
FROM
@ -99,6 +100,7 @@ GROUP BY
,pc.branding
,pc.accs
,pc.suffix
,2 col
,pc.pckg2 pckg
,pc.vol2_uom vol_uom
FROM
@ -110,6 +112,7 @@ GROUP BY
,pc.branding
,pc.accs
,pc.suffix
,3 col
,pc.pckg3 pckg
,pc.vol3_uom vol_uom
FROM
@ -124,6 +127,8 @@ SELECT
,up.accs
,up.suffix
,up.pckg
--retain this column to re-pivot back to original format
,up.col
,up.vol_uom
,row_number() OVER (PARTITION BY up.stlc, up.coltier, up.branding, up.accs, up.suffix ORDER BY item DESC) seq
,item
@ -138,7 +143,7 @@ FROM
AND COALESCE(i.SUFFIX,'') = up.suffix
AND APLNT <> 'I'
)
----compile all the conversions into a single json object
----compile all the conversions into a single json object----------------------
,items AS (
SELECT
jsonb_agg( DISTINCT
@ -154,6 +159,7 @@ FROM
seq = 1
AND COALESCE(item,'') <> ''
)
--do the conversion-------------------------------------------------------------
, conversion AS (
SELECT
rslt.*
@ -161,7 +167,8 @@ FROM
items
JOIN LATERAL rlarp.uom_array(conv) rslt ON true
)
--SELECT * FROM conversion
--SELECT * FROM unpivot ORDER BY stlc
----join conversion back to data initial part link------------------------------
SELECT
i.*
,c.*