------------real history on target scenario------------
WITH
partcodes AS (
    SELECT 
        item
        ,'v1:' || COALESCE(i.coltier,'') || '.' || COALESCE(substring(i.branding,1,1),'') || '.' || coalesce(i.uomp,'') || '.' || coalesce(i.suffix,'') || '.' || coalesce(i.accs_ps,'') v1dataseg
        ,_ds.dataseg v0dataseg
        ,part_group
    FROM
        "CMS.CUSLG".itemm i
        LEFT OUTER JOIN rlarp.molds m ON
            m.stlc = i.stlc
        LEFT OUTER JOIN _ds ON
            _ds.colgrp = i.colgrp
            AND _ds.brand = i.branding
    WHERE 
        i.item ~ 'TUH1000'
        AND part_group IS NOT NULL
)
,custs AS (
    SELECT
        dba
    FROM  
        rlarp.cust c 
    WHERE
        c.dba ~ 'DIAMOND '
)
,allscenarios AS (
    SELECT DISTINCT
        jsonb_build_object('mold',part_group,'cust',dba) sc
    FROM
        partcodes
        CROSS JOIN custs
)
,v1scenarios AS (
    SELECT DISTINCT
        jsonb_build_object('mold',part_group,'cust',dba,'v1dataseg',v1dataseg) sc
    FROM
        partcodes
        CROSS JOIN custs
)
,v0scenarios AS (
    SELECT DISTINCT
        jsonb_build_object('mold',part_group,'cust',dba,'v0dataseg',v0dataseg) sc
    FROM
        partcodes
        CROSS JOIN custs
)
,hist AS (
    SELECT  
        s.sc
        ,p.gset
        ,(SELECT string_agg(substring(ok,1,2),'') FROM (SELECT * FROM jsonb_object_keys(p.gset) jok(ok)) x )
        ,p.season
    FROM 
        allscenarios s
        LEFT OUTER JOIN rlarp.price_pool_dev p ON
            p.gset @> s.sc
)
SELECT * FROM hist
