WITH rows AS (
SELECT
    ---------customer info-----------------
    bill_cust_descr
    ,billto_group
    ,ship_cust_descr
    ,shipto_group
    ,quota_rep_descr
    ,director
    ,segm
    ,substance
    ,chan
    ,chansub
    ---------product info------------------
    ,majg_descr
    ,ming_descr
    ,majs_descr
    ,mins_descr
    --,brand
    --,part_family
    ,part_group
    ,branding
    --,color
    ,part_descr
    ---------dates-------------------------
    ,order_season
    ,order_month
    ,ship_season
    ,ship_month
    ,request_season
    ,request_month
    ,promo
    --------values-------------------------
    ,sum(value_loc) value_loc
    ,sum(value_usd) value_usd
    ,sum(cost_loc) cost_loc
    ,sum(cost_usd) cost_usd
    ,sum(units) units
    ,sum(pounds) pounds
    ,version
    ,iter
    ,logid
    ,tag
    ,comment
FROM
    rlarp.osm_pool
WHERE
    where_clause
GROUP BY
        ---------customer info-----------------
    bill_cust_descr
    ,billto_group
    ,ship_cust_descr
    ,shipto_group
    ,quota_rep_descr
    ,director
    ,segm
    ,substance
    ,chan
    ,chansub
    ---------product info------------------
    ,majg_descr
    ,ming_descr
    ,majs_descr
    ,mins_descr
    --,brand
    --,part_family
    ,part_group
    ,branding
    --,color
    ,part_descr
    ---------dates-------------------------
    ,order_season
    ,order_month
    ,ship_season
    ,ship_month
    ,request_season
    ,request_month
    ,promo
    ,version
    ,iter
    ,logid
    ,tag
    ,comment
    ,substance
ORDER BY
    logid ASC
)
SELECT
    json_agg(row_to_json(rows)) x
FROM
    rows
