with SEG AS ( SELECT GLEC ,SEGM FROM ( VALUES ('1CU','Retail'), ('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) ) ,fp as ( SELECT od.part, sum(value_usd) sales, sum(cost_usd) scost FROM rlarp.osm_pool od LEFT OUTER JOIN rlarp.itemmv m ON m.item = od.part WHERE m.item is null AND coalesce(od.part,'') <> '' GROUP BY od.part ) ,repl AS ( SELECT * FROM (VALUES ('ANP00001A39','ANP00001A39C003LRDBP','New 2020'), ('ANP12000A39','ANP12000A39C001LRCOM','New 2020'), ('ANP15000A39','ANP15000A39C001LRCON','New 2020'), ('ANP17500A39','ANP17500A39C001LRCOO','New 2020'), ('ARP06000DE3','ARP06000DE3C012LRDBQ','New 2020'), ('ARP06000P26','ARP06000P26C012LRDBR','New 2020'), ('ARP06000P80','ARP06000P80C012LRDBS','New 2020'), ('ARP06000P81','ARP06000P81C012LRDBT','New 2020'), ('ARP08000DE3','ARP08000DE3C012LRDBU','New 2020'), ('ARP08000P26','ARP08000P26C012LRDBV','New 2020'), ('ARP08000P80','ARP08000P80C012LRDBW','New 2020'), ('ARP08000P81','ARP08000P81C012LRDBX','New 2020'), ('ARP10000DE3','ARP10000DE3C010LRDBY','New 2020'), ('ARP10000P26','ARP10000P26C010LRDBZ','New 2020'), ('ARP10000P80','ARP10000P80C010LRDCA','New 2020'), ('ARP10000P81','ARP10000P81C010LRDCB','New 2020'), ('ARP12000DE3','ARP12000DE3C010LRDCC','New 2020'), ('ARP12000P26','ARP12000P26C010LRDCD','New 2020'), ('ARP12000P80','ARP12000P80C010LRDCE','New 2020'), ('ARP12000P81','ARP12000P81C010LRDCF','New 2020'), ('ARP16000DE3','ARP16000DE3C008LRDCG','New 2020'), ('ARP16000P26','ARP16000P26C008LRDCH','New 2020'), ('ARP16000P80','ARP16000P80C008LRDCI','New 2020'), ('ARP16000P81','ARP16000P81C008LRDCJ','New 2020'), ('ASP06000DE3','ASP06000DE3C012LRDCK','New 2020'), ('ASP06000P26','ASP06000P26C012LRDCL','New 2020'), ('ASP06000P80','ASP06000P80C012LRDCM','New 2020'), ('ASP06000P81','ASP06000P81C012LRDCN','New 2020'), ('ASP08000DE3','ASP08000DE3C012LRDCO','New 2020'), ('ASP08000P26','ASP08000P26C012LRDCP','New 2020'), ('ASP08000P80','ASP08000P80C012LRDCQ','New 2020'), ('ASP08000P81','ASP08000P81C012LRDCR','New 2020'), ('ASP10000DE3','ASP10000DE3C010LRDCS','New 2020'), ('ASP10000P26','ASP10000P26C010LRDCT','New 2020'), ('ASP10000P80','ASP10000P80C010LRDCU','New 2020'), ('ASP10000P81','ASP10000P81C010LRDCV','New 2020'), ('ASP12000DE3','ASP12000DE3C010LRDCW','New 2020'), ('ASP12000P26','ASP12000P26C010LRDCX','New 2020'), ('ASP12000P80','ASP12000P80C010LRDCY','New 2020'), ('ASP12000P81','ASP12000P81C010LRDCZ','New 2020'), ('ASP16000DE3','ASP16000DE3C008LRDDB','New 2020'), ('ASP16000P26','ASP16000P26C008LRDDC','New 2020'), ('ASP16000P80','ASP16000P80C008LRDDE','New 2020'), ('ASP16000P81','ASP16000P81C008LRDDF','New 2020'), ('CAR12000P26','CAR13000P261004LRDBE','New 2020'), ('CAR12000P80','CAR13000P801004LRDBF','New 2020'), ('CAR15000P26','CAR16000P261004LRDBG','New 2020'), ('CAR15000P80','CAR16000P801004LRDBH','New 2020'), ('CAS12000P26','CAS12000P261004LRDBI','New 2020'), ('CAS12000P80','CAS12000P801004LRDBJ','New 2020'), ('CAS15000P26','CAS15000P261004LRDBK','New 2020'), ('CAS15000P80','CAS15000P801004LRDBL','New 2020'), ('CNA06000A10','CNA06000A10C024LRCJJ','New 2020'), ('CNA06000A42','CNA06000A42C024LRCDC','New 2020'), ('CNA06000BG3','CNA06000BG3C024LRCJK','New 2020'), ('CNA06000F89','CNA06000F89C024LRCJL','New 2020'), ('CNA08000A10','CNA08000A10C024LRCJM','New 2020'), ('CNA08000A42','CNA08000A42C024LRBXH','New 2020'), ('CNA08000BG3','CNA08000BG3C024LRCJN','New 2020'), ('CNA08000F89','CNA08000F89C024LRCJO','New 2020'), ('CNA10000A10','CNA10000A10C012LRCJP','New 2020'), ('CNA10000BG3','CNA10000BG3C012LRCJQ','New 2020'), ('CNA10000F89','CNA10000F89C012LRCJR','New 2020'), ('CNA12000A10','CNA12000A10C012LRCJS','New 2020'), ('CNA12000BG3','CNA12000BG3C012LRCJT','New 2020'), ('CNA12000F89','CNA12000F89C012LRCJU','New 2020'), ('ECA06000B78','ECA06000B781028LRCJV','New 2020'), ('ECA06000DE2','ECA06000DE21028LRCJW','New 2020'), ('ECA08000B78','ECA08000B781024LRCJX','New 2020'), ('ECA08000DE2','ECA08000DE21024LRCJY','New 2020'), ('ECA10000B78','ECA10000B781020LRCJZ','New 2020'), ('ECA10000DE2','ECA10000DE21020LRCKA','New 2020'), ('ECA12000B78','ECA12000B781016LRCKB','New 2020'), ('ECA12000DE2','ECA12000DE21016LRCKC','New 2020'), ('ECA16000B78','ECA16000B781010LRCKD','New 2020'), ('ECA16000DE2','ECA16000DE21010LRCKE','New 2020'), ('ECA20000B78','ECA20000B781006LRCKF','New 2020'), ('ECA20000DE2','ECA20000DE21006LRCKG','New 2020'), ('GAB12000BG3','GAB12000BG3C012LRCKH','New 2020'), ('GAB12000DE2','GAB12000DE2C012LRCKI','New 2020'), ('GAB14000BG3','GAB14000BG3C012LRCKJ','New 2020'), ('GAB14000DE2','GAB14000DE2C012LRCKK','New 2020'), ('GAB18000BG3','GAB18000BG3C006LRCKL','New 2020'), ('GAB18000DE2','GAB18000DE2C006LRCKM','New 2020'), ('GAB22000BG3','GAB22000BG3C006LRCKN','New 2020'), ('GAB22000DE2','GAB22000DE2C006LRCKO','New 2020'), ('GDP08000BG3','GDP08000BG3C012LRCOJ','New 2020'), ('GDP08000DE2','GDP08000DE2C012LRCOK','New 2020'), ('LIA06000B78','LIA06000B78C024LRCKP','New 2020'), ('LIA06000DE2','LIA06000DE2C024LRCKQ','New 2020'), ('LIA08000B78','LIA08000B78C024LRCKR','New 2020'), ('LIA08000DE2','LIA08000DE2C024LRCKS','New 2020'), ('LIA10000B78','LIA10000B78C012LRCKT','New 2020'), ('LIA10000DE2','LIA10000DE2C012LRCKU','New 2020'), ('LIA12000B78','LIA12000B78C012LRCKV','New 2020'), ('LIA12000DE2','LIA12000DE2C012LRCKW','New 2020'), ('LIA14000B78','LIA14000B78C012LRCKX','New 2020'), ('LIA14000DE2','LIA14000DE2C012LRCKY','New 2020'), ('LIA16000B78','LIA16000B78C012LRCKZ','New 2020'), ('LIA16000DE2','LIA16000DE2C012LRCLA','New 2020'), ('LIA20000B78','LIA20000B78C006LRCLB','New 2020'), ('LIA20000DE2','LIA20000DE2C006LRCLC','New 2020'), ('LIA24000B78','LIA24000B78C006LRCLD','New 2020'), ('LIA24000DE2','LIA24000DE2C006LRCLE','New 2020'), ('MSA12001A42','MSA12001A421008LRCLF','New 2020'), ('MSA12001B78','MSA12001B781008LRCLG','New 2020'), ('MSA12001DE2','MSA12001DE21008LRCLH','New 2020'), ('MSA16001A42','MSA16001A42C004LRCLI','New 2020'), ('MSA16001B78','MSA16001B78C004LRCLJ','New 2020'), ('MSA16001DE2','MSA16001DE2C004LRCLK','New 2020'), ('MSA20001A42','MSA20001A42C004LRCLL','New 2020'), ('MSA20001B78','MSA20001B78C004LRCLM','New 2020'), ('MSA20001DE2','MSA20001DE2C004LRCLN','New 2020'), ('SEA07001A42','SEA07001A421008LRCLP','New 2020'), ('SEA07001B78','SEA07001B781008LRCLQ','New 2020'), ('SEA07001DE2','SEA07001DE21008LRCLR','New 2020'), ('SEA09001A42','SEA09001A421008LRCLT','New 2020'), ('SEA09001B78','SEA09001B781008LRCLU','New 2020'), ('SEA09001DE2','SEA09001DE21008LRCLV','New 2020'), ('SEA12001A42','SEA12001A421006LRCLX','New 2020'), ('SEA12001B78','SEA12001B781006LRCLY','New 2020'), ('SEA12001DE2','SEA12001DE21006LRCLZ','New 2020'), ('SEA14001A42','SEA14001A421006LRCMB','New 2020'), ('SEA14001B78','SEA14001B781006LRCMC','New 2020'), ('SEA14001DE2','SEA14001DE21006LRCMD','New 2020'), ('SEA16000A42','SEA16001A421006LRCMF','New 2020'), ('SEA16000B78','SEA16001B781006LRCMG','New 2020'), ('SEA16000DE2','SEA16001DE21006LRCMH','New 2020'), ('SKR12000A56','SKR12000A56C010LRDDI','New 2020'), ('SKR12000E66','SKR12000E66C010LRDDJ','New 2020'), ('SKR12000G27','SKR12000G27C010LRDDK','New 2020'), ('SKR16000A56','SKR16000A56C008LRDDL','New 2020'), ('SKR16000E66','SKR16000E66C008LRDDM','New 2020'), ('SKR16000G27','SKR16000G27C008LRDDN','New 2020'), ('SKS12000A56','SKS12000A56C010LRDDO','New 2020'), ('SKS12000E66','SKS12000E66C010LRDDP','New 2020'), ('SKS12000G27','SKS12000G27C010LRDDQ','New 2020'), ('SKS16000A56','SKS16000A56C008LRDDR','New 2020'), ('SKS16000E66','SKS16000E66C008LRDDS','New 2020'), ('SKS16000G27','SKS16000G27C008LRDDT','New 2020'), ('SLI06000B78','SLI06000B78C024LRCMI','New 2020'), ('SLI06000DE2','SLI06000DE2C024LRCMJ','New 2020'), ('SLI08000B78','SLI08000B78C024LRCMK','New 2020'), ('SLI08000DE2','SLI08000DE2C024LRCML','New 2020'), ('SLI10000B78','SLI10000B78C012LRCMM','New 2020'), ('SLI10000DE2','SLI10000DE2C012LRCMN','New 2020'), ('SLI12000B78','SLI12000B78C012LRCMO','New 2020'), ('SLI12000DE2','SLI12000DE2C012LRCMP','New 2020'), ('SLI14000B78','SLI14000B78C012LRCMQ','New 2020'), ('SLI14000DE2','SLI14000DE2C012LRCMR','New 2020'), ('SLI16000B78','SLI17000B78C012LRCMS','New 2020'), ('SLI17000DE2','SLI17000DE2C012LRCMT','New 2020'), ('SLI20000B78','SLI20000B78C006LRCMU','New 2020'), ('SLI20000DE2','SLI20000DE2C006LRCMV','New 2020'), ('SLI24000B78','SLI24000B78C006LRCMW','New 2020'), ('SLI24000DE2','SLI24000DE2C006LRCMX','New 2020'), ('SVN18000A10','SVN18000A10C006LRCMY','New 2020'), ('SVN18000A42','SVN18000A42C006LRCMZ','New 2020'), ('SVN24000A10','SVN24000A10C006LRCNA','New 2020'), ('SVN24000A42','SVN24000A42C006LRCNB','New 2020'), ('SVN30000A10','SVN30000A10C006LRCNC','New 2020'), ('SVN30000A42','SVN30000A42C006LRCND','New 2020'), ('VNP18000A10','VNP18000A10C006LRCNE','New 2020'), ('VNP18000A42','VNP18000A42C006LRCNF','New 2020'), ('VNP24000A10','VNP24000A10C006LRCNG','New 2020'), ('VNP24000A42','VNP24000A42C006LRCNH','New 2020'), ('VNP30000A10','VNP30000A10C006LRCNI','New 2020'), ('VNP30000A42','VNP30000A42C006LRCNJ','New 2020'), ('WBP16000P82','WBP16000P82C008LRDDG','New 2020'), ('ROS15500A42','ROS15500A42C012LRDFA','New 2021'), ('SRO15500A42','SRO15500A42C012LRDFB','New 2021'), ('CSB06000A58','CSB06000A58C020LRDHO','New 2021'), ('CSB06000E67','CSB06000E67C020LRDHM','New 2021'), ('CSB06000G28','CSB06000G28C020LRDHN','New 2021'), ('CSB08000A58','CSB08000A58C020LRDHU','New 2021'), ('CSB08000E67','CSB08000E67C020LRDHS','New 2021'), ('CSB08000G28','CSB08000G28C020LRDHT','New 2021'), ('CNA06000A02','CNA06000A02C024LRDGU','New 2021'), ('CNA06000CXX','CNA06000CXXC024LRDGV','New 2021'), ('CNA06000DE1','CNA06000DE1C024LRDGW','New 2021'), ('CNA08000A02','CNA08000A02C024LRDGX','New 2021'), ('CNA08000CXX','CNA08000CXXC024LRDGY','New 2021'), ('CNA08000DE1','CNA08000DE1C024LRDGZ','New 2021'), ('CNA10000A02','CNA10000A02C012LRDHA','New 2021'), ('CNA10000CXX','CNA10000CXXC012LRDHB','New 2021'), ('CNA10000DE1','CNA10000DE1C012LRDHC','New 2021'), ('CNA12000A02','CNA12000A02C012LRDHD','New 2021'), ('CNA12000CXX','CNA12000CXXC012LRDHE','New 2021'), ('CNA12000DE1','CNA12000DE1C012LRDHF','New 2021'), ('DFB06000A58','DFB06000A58C024LRDHR','New 2021'), ('DFB06000E67','DFB06000E67C024LRDHP','New 2021'), ('DFB06000G28','DFB06000G28C024LRDHQ','New 2021'), ('DFB08000A58','DFB08000A58C024LRDHX','New 2021'), ('DFB08000E67','DFB08000E67C024LRDHV','New 2021'), ('DFB08000G28','DFB08000G28C024LRDHW','New 2021'), ('ECH12000A42','ECH12000A421010LRDFM','New 2021'), ('ECH12000E21','ECH12000E211010LRDFK','New 2021'), ('ECH12000G18','ECH12000G181010LRDFL','New 2021'), ('ECA06000A02','ECA06000A021028LRDEJ','New 2021'), ('ECA06000A42','ECA06000A421028LRDEI','New 2021'), ('ECA08000A02','ECA08000A021024LRDEL','New 2021'), ('ECA08000A42','ECA08000A421024LRDEK','New 2021'), ('ECA10000A02','ECA10000A021020LRDEM','New 2021'), ('ECA10000A42','ECA10000A421020LRDEN','New 2021'), ('ECA12000A02','ECA12000A021016LRDEO','New 2021'), ('ECA12000A42','ECA12000A421016LRDEP','New 2021'), ('ECA16000A02','ECA16000A021010LRDES','New 2021'), ('ECA16000A42','ECA16000A421010LRDET','New 2021'), ('ECA20000A02','ECA20000A021006LRDEW','New 2021'), ('ECA20000A42','ECA20000A421006LRDEX','New 2021'), ('ECI12000A42','ECI12000A421010LRDFP','New 2021'), ('ECI12000E21','ECI12000E211010LRDFN','New 2021'), ('ECI12000G18','ECI12000G181010LRDFO','New 2021'), ('ECP06000A02','ECP06000A021028LRDGK','New 2021'), ('ECP06000A42','ECP06000A421028LRDGM','New 2021'), ('ECP06000DE2','ECP06000DE21028LRDGL','New 2021'), ('ECP06000E35','ECP06000E351028LRDGJ','New 2021'), ('ECP06000G18','ECP06000G181028LRDGI','New 2021'), ('ECP08000A02','ECP08000A021024LRDGP','New 2021'), ('ECP08000A42','ECP08000A421024LRDGR','New 2021'), ('ECP08000DE2','ECP08000DE21024LRDGQ','New 2021'), ('ECP08000E35','ECP08000E351024LRDGO','New 2021'), ('ECP08000G18','ECP08000G181024LRDGN','New 2021'), ('ECP10000A02','ECP10000A021020LRDGA','New 2021'), ('ECP10000A42','ECP10000A421020LRDGC','New 2021'), ('ECP10000DE2','ECP10000DE21020LRDGB','New 2021'), ('ECP10000E35','ECP10000E351020LRDFZ','New 2021'), ('ECP10000G18','ECP10000G181020LRDFY','New 2021'), ('ECP12000A02','ECP12000A021016LRDGF','New 2021'), ('ECP12000A42','ECP12000A421016LRDGH','New 2021'), ('ECP12000DE2','ECP12000DE21016LRDGG','New 2021'), ('ECP12000E35','ECP12000E351016LRDGE','New 2021'), ('ECP12000G18','ECP12000G181016LRDGD','New 2021'), ('ECW24000A10','ECW24000A101006LRDFQ','New 2021'), ('ECW24000A42','ECW24000A421006LRDFT','New 2021'), ('ECW24000E35','ECW24000E351006LRDFS','New 2021'), ('ECW24000G18','ECW24000G181006LRDFR','New 2021'), ('ECW30000A10','ECW30000A101006LRDFU','New 2021'), ('ECW30000A42','ECW30000A421006LRDFX','New 2021'), ('ECW30000E35','ECW30000E351006LRDFW','New 2021'), ('ECW30000G18','ECW30000G181006LRDFV','New 2021'), ('GAB12000A42','GAB12000A42C012LRDFC','New 2021'), ('GAB14000A42','GAB14000A42C012LRDFD','New 2021'), ('GAB22000A42','GAB22000A42C006LRDFE','New 2021'), ('GDP08000A10','GDP08000A10C012LRDFF','New 2021'), ('HBM12000H13','HBM12000H136010LRDIB','New 2021'), ('HBM12000H26','HBM12000H226010LRDIC','New 2021'), ('HBM12000H22','HBM12000H266010LRDIA','New 2021'), ('PA.08005B71',CAST(NULL AS VARCHAR(255)),'New 2021'), ('PA.08005E22','PA.08005E22C250LYHQO','New 2021'), ('SPP08000A58','SPP08000A581004LRDHZ','New 2021'), ('SPP08000E67','SPP08000E671004LRDHY','New 2021'), ('PZA09000YB5','PZA09000YB5C012LRDFG','New 2021'), ('PZA12000YB5','PZA12000YB5C012LRDFH','New 2021'), ('PZA16000YB5','PZA16000YB5C006LRDFI','New 2021'), ('PZA20000YB5','PZA20000YB5C006LRDFJ','New 2021'), ('SSP02500A58','SSP02500A58C020LRDHI','New 2021'), ('SSP02500E67','SSP02500E67C020LRDHG','New 2021'), ('SSP02500G28','SSP02500G28C020LRDHH','New 2021'), ('SSP03000A58','SSP03000A58C020LRDHL','New 2021'), ('SSP03000E67','SSP03000E67C020LRDHJ','New 2021'), ('SSP03000G28','SSP03000G28C020LRDHK','New 2021'), ('RZ.MT160G18',CAST(NULL AS VARCHAR(255)),'New 2021'), ('RZ.MT160A42',CAST(NULL AS VARCHAR(255)),'New 2021'), ('RZ.MT160XXX',CAST(NULL AS VARCHAR(255)),'New 2021'), ('RZ.WC2G3B66','RZ.WC2G3B66C020LRCOT','New 2021') ) x(f11,fp,note) ) , fin AS ( SELECT fp.part, fp.sales, min(item) item, repl.fp, COALESCE(repl.fp,min(item)) pick FROM fp LEFT OUTER JOIN rlarp.itemmv m ON substring(m.item,1,11) = substring(fp.part,1,11) LEFT OUTER JOIN repl ON repl.f11 = substring(fp.part,1,11) GROUP BY fp.part, fp.sales, repl.fp ORDER BY fp.sales desc, fp.part ) --SELECT * FROM fin; /* ,upd AS ( UPDATE rlarp.osm_pool o SET part = fin.pick FROM fin WHERE fin.part = o.part RETURNING * ) SELECT * from upd; */ --SELECT * FROM rlarp.osm_pool o inner join fin on fin.part = o.part ; select * from fin; ROLLBACK; /* ,ins AS ( SELECT p.fspr ,p.plnt ,p.promo ,p.terms ,p.bill_cust_descr ,p.ship_cust_descr ,p.dsm ,p.quota_rep_descr ,p.director ,p.billto_group ,p.shipto_group ,p.chan ,p.chansub ,p.chan_retail ,fin.pick ,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 ,p.fs_line ,p.r_currency ,p.r_rate ,p.c_currency ,p.c_rate ,p.units ,p.value_loc ,p.value_usd ,p.cost_loc ,p.cost_usd ,p.calc_status ,p.flag ,p.order_date ,p.order_month ,p.order_season ,p.request_date ,p.request_month ,p.request_season ,p.ship_date + INTERVAL '1 year' ,ship_month ,ship_season ,'b21' AS version ,'fake parts' iter ,logl.id logid ,logl.doc->>'tag' tag ,logl.doc->>'message' "comment" ,logl.doc->>'type' module FROM rlarp.osm_pool p INNER JOIN fin ON fin.part = p.part LEFT OUTER JOIN rlarp.itemm i ON i.item = fin.pick LEFT OUTER JOIN seg ON seg.glec = i.glec; */