WITH
--getj AS (
--    SELECT
--        jsonb_build_object('mold',stlc) doc
--    FROM
--        "CMS.CUSLG".itemm 
--    WHERE 
--        item = $1
--)
agg AS (
    SELECT 
         gset
        ,agglevel
        ,season
    FROM 
        rlarp.price_pool_dev 
    WHERE  
        --gset @> '{"mold":"XNS0T1G3"}'::jsonb 
        gset @> jsonb_build_object(
            'mold','TUH10000',
            'vers','A'
        )
        AND (
            gset->>'cust' ~ 'DIAMOND'
            OR NOT gset ? 'cust'
        )
    ORDER BY 
        agglevel ASC
)
SELECT
    jsonb_build_object('agg',agglevel )
    || jsonb_build_object(
        'data',
        jsonb_agg(gset || jsonb_build_object('season',season))
    ) data
FROM
    agg
GROUP BY
    agglevel
LIMIT 10
--SELECT 
--     jsonb_pretty(agglevel) agglevel
--    ,jsonb_pretty(gset)     gset
--    ,jsonb_pretty(season)   season
--FROM  
--    agg
