BEGIN;

WITH
SEG AS (
    SELECT
        GLEC
        ,SEGM
    FROM
        (
            VALUES
            ('1SU','Sustainable'),
            ('1CU','Sustainable'),
            ('1GR','Greenhouse'),
            ('1NU','Nursery'),
            ('1RE','Retail'),
            ('2WI','Greenhouse'),
            ('3BM','Other'),
            ('3CO','Other'),
            ('3PE','Other'),
            ('3PP','Other'),
            ('4CO','Other'),
            ('4RA','Other'),
            ('9MI','Other'),
            ('9SA','Other'),
            ('9TO','Other')
        ) X(GLEC, SEGM)
)
,fin AS (
SELECT DISTINCT
    o.part
    ,i.item||coalesce(' - '||i.descr,'') part_descr
    ,i.stlc part_group
    ,i.branding
    ,i.majg||' - '||i.majgd majg_descr
    ,i.ming||' - '||i.mingd ming_descr
    ,i.majs||' - '||i.majsd majs_descr
    ,i.mins||' - '||i.minsd mins_descr
    ,seg.segm
    ,CASE WHEN i.majg = '610' THEN 'Fiber' ELSE 'Plastic' END substance
FROM
    rlarp.osm_pool o
    LEFT OUTER JOIN rlarp.itemmv i ON
        i.item = o.part
    LEFT OUTER JOIN seg ON
        seg.glec = i.glec
)
UPDATE
    rlarp.osm_pool o
SET
     part_descr   = fin.part_descr
    ,part_group   = fin.part_group
    ,branding     = fin.branding
    ,majg_descr   = fin.majg_descr
    ,ming_descr   = fin.ming_descr
    ,majs_descr   = fin.majs_descr
    ,mins_descr   = fin.mins_descr
    ,segm         = fin.segm
    ,substance    = fin.substance
FROM
    fin
WHERE
    fin.part = o.part;

COMMIT;
