-----------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 ~ $1
        AND part_group IS NOT NULL
)
,customers AS (
    SELECT
        dba
    FROM  
        rlarp.cust c 
    WHERE
        c.dba ~ $2
)
,allscenarios AS (
    SELECT DISTINCT
        jsonb_build_object('baseitem',part_group,'customer',dba) sc
    FROM
        partcodes
        CROSS JOIN customers
)
,v1scenarios AS (
    SELECT DISTINCT
        jsonb_build_object('baseitem',part_group,'customer',dba,'v1dataseg',v1dataseg) sc
    FROM
        partcodes
        CROSS JOIN customers
)
,v0scenarios AS (
    SELECT DISTINCT
        jsonb_build_object('baseitem',part_group,'customer',dba,'v0dataseg',v0dataseg) sc
    FROM
        partcodes
        CROSS JOIN customers
)
,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 ) agglevel
        ,p.season
    FROM 
        allscenarios s
        LEFT OUTER JOIN rlarp.price_pool_r1 p ON
            p.gset @> s.sc
)
SELECT * FROM hist
