From 16be2ad2649acc747a3c9c111c7cedb0df8efd88 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Fri, 10 Nov 2023 11:44:21 -0500 Subject: [PATCH] vault backup: 2023-11-10 11:44:21 --- sql/get_guidance_dseg.pg.sql | 55 +++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/sql/get_guidance_dseg.pg.sql b/sql/get_guidance_dseg.pg.sql index 5804f7f..2cf552d 100644 --- a/sql/get_guidance_dseg.pg.sql +++ b/sql/get_guidance_dseg.pg.sql @@ -36,13 +36,14 @@ BEGIN ----------------base product-------------------------------- + SELECT - m.part_group - ,min(i.item) item - ,i.stlc - ,i.v1ds - ,i.v0ds - ,jsonb_strip_nulls(jsonb_build_object('assc',CASE WHEN i.assc <> '' THEN i.assc ELSE null::text END,'majg',i.majg::int,'coltier',i.coltier)) idxk + part_group + ,item + ,stlc + ,v1ds + ,v0ds + ,idxk INTO _mold ,_item @@ -50,20 +51,34 @@ BEGIN ,_v1ds ,_v0ds ,_iidx - FROM - "CMS.CUSLG".itemmv i - INNER JOIN rlarp.molds m ON - m.stlc = i.stlc - WHERE - i.stlc = _stlc - AND i.v1ds = _dseg - GROUP BY - m.part_group - ,i.stlc - ,i.v1ds - ,i.v0ds - ,jsonb_strip_nulls(jsonb_build_object('assc',CASE WHEN i.assc <> '' THEN i.assc ELSE null::text END,'majg',i.majg::int,'coltier',i.coltier)); - _rslt := jsonb_build_object('mold',_mold,'v1ds',_v1ds,'v0ds',_v0ds,'stlc',_stlc)||_iidx; + FROM + ( + SELECT + m.part_group + ,min(i.item) item + ,i.stlc + ,i.v1ds + ,i.v0ds + ,jsonb_strip_nulls(jsonb_build_object('assc',CASE WHEN i.assc <> '' THEN i.assc ELSE null::text END,'majg',i.majg::int,'coltier',i.coltier)) idxk + ,CASE WHEN i.v1ds = _v1ds THEN 1 ELSE 0 END prefer + FROM + "CMS.CUSLG".itemmv i + INNER JOIN rlarp.molds m ON + m.stlc = i.stlc + WHERE + i.stlc = _stlc + GROUP BY + m.part_group + ,i.stlc + ,i.v1ds + ,i.v0ds + ,jsonb_strip_nulls(jsonb_build_object('assc',CASE WHEN i.assc <> '' THEN i.assc ELSE null::text END,'majg',i.majg::int,'coltier',i.coltier)) + ,CASE WHEN i.v1ds = _v1ds THEN 1 ELSE 0 END + ) best + ORDER BY + prefer DESC + LIMIT 1; + _rslt := jsonb_build_object('mold',_mold,'v1ds',_v1ds,'v0ds',_v0ds,'stlc',_stlc,'item',_item,'desg',_dseg)||_iidx; RAISE NOTICE 'item data %', _iidx; ----------------channel-------------------------------------