WITH nc AS (
select 
    part
    ,plnt
    , to_char(sum(fb_val_loc * r_rate),'9,999,999') sales
    , to_char(sum(fb_qty),'9,999,999') units 
from
    rlarp.osm_fcpool 
where 
    (
        (
            fb_qty <> 0 
            and coalesce(fb_cst_loc,0) = 0 
        )
        or plnt is null
    )
    and version = 'b20' 
group by 
    part
    ,plnt
)
SELECT
    nc.*
    ,v6stat
    ,coalesce(cgstcs, chstcs,y0stcs) stcs
FROM
    nc
    left outer join lgdat.stka ON
        v6part = part
        and v6plnt = plnt
    left outer join lgdat.icstm ON
        cgpart = part
        and cgplnt = plnt
    left outer join lgdat.icstp ON
        chpart = part
        and chplnt = plnt
    left outer join lgdat.icstr ON
        y0part = part
        and y0plnt = plnt
