From 412f0ec082c2b99ec6677b3a269f0c2e54d386c9 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Mon, 11 Jan 2021 11:05:44 -0500 Subject: [PATCH] add column number to facilitate pivoting --- db_schema/extract_for_customer_facing.pg.sql | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/db_schema/extract_for_customer_facing.pg.sql b/db_schema/extract_for_customer_facing.pg.sql index 861d4f3..8092ae6 100644 --- a/db_schema/extract_for_customer_facing.pg.sql +++ b/db_schema/extract_for_customer_facing.pg.sql @@ -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.*