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