WITH
newc AS (
    SELECT * FROM (VALUES
    ('A GARDEN CENTER','30053 - SKYE SAWYER'),
    ('FILANOWSKI & SONS','10042 - JEREMY RHAULT'),
    ('GIESEKE ENTERPRISES','10041 - TAMARA RISKEN'),
    ('HUNTER ROAD GREENHOUSES','40039 - DAVE BODO'),
    ('JAMPLAST INC','13027 - STEVE RUSTEBERG'),
    ('MOUNT PLYMOUTH GREENHOUSES','10046 - DUSTIN KUYKENDOLL 2'),
    ('PINCKARD PLANT FARM INC','10041 - TAMARA RISKEN'),
    ('SHERMAN PLASTICS','13027 - STEVE RUSTEBERG'),
    ('SPRING VALLEY NURSERY','12028 - JACOB WILKINSON'),
    ('A&R SPADA FARMS LLC','30053 - SKYE SAWYER'),
    ('CI FARMS LLC','30054 - JASON MOTTIN'),
    ('GOLDEN EAGLE TREES','40042 - TOM MORRIS'),
    ('TSC NURSERY SALES LTD','40042 - TOM MORRIS')
    ) x(orphan,rep)
)
,profile AS (
    SELECT * FROM rlarp.osm WHERE shipgrp IN (select distinct orphan FROM newc ) AND oseas >= 2015
)
-- SELECT * FROM profile
,checkc AS (
SELECT shipgrp, min(oseas) oseas FROM profile GROUP BY shipgrp
)
-- select * from checkc
-- SELECT cust, sum(fb_qty) FROM newc LEFT OUTER JOIN profile ON shipgrp = cust GROUP by cust
----grouped prices-------------
,npgrp AS (
	SELECT
		o.bill_dba 
		,o.ship_dba
        ,o.dsm
        ,i.partgroup
        ,i.pricegroup
		,o.part
		,o.odate
		,o.qtyord
		,i.nwht * CASE WHEN i.nwun = 'KG' THEN 2.2 ELSE 1 END lbs
		,i.mpck pltq
		,ROUND(o.sales_usd / o.qty,5) price
		,(o.pricing->>'guidance_price')::NUMERIC guidance
	    ,(o.pricing->>'floor'  )::numeric(15,5) floor        
	    ,(o.pricing->>'ceiling')::numeric(15,5) ceiling   
	    ,(o.pricing->>'uselimits')::boolean uselimits
	FROM
	 	rlarp.osm_stack o
	 	LEFT OUTER JOIN "CMS.CUSLG".itemm i ON 
	 		i.item = o.part
        INNER JOIN checkc c ON 
            c.shipgrp = o.customer
            AND c.oseas = o.oseas
	WHERE 
		VERSION = 'Actual' 
		-- AND o.oseas >= 2018
		AND calc_status <> 'CANCELED' 
		AND o.fs_line = '41010'
		AND o.part <> ''
		AND substring(o.glec,1,1) <= '2'
		AND o.qty <> 0
        AND o.sseas IS NOT NULL
        -- AND ROUND(o.sales_usd / o.qty,5) 
        -- AND customer in (select distinct renamed FROM newc )
	GROUP BY 
		o.bill_dba 
		,o.ship_dba
        ,o.dsm
        ,i.partgroup
        ,i.pricegroup
		,o.part
		,o.odate
		,o.qtyord
		,o.pounds
		,o.pallets
		,ROUND(o.sales_usd / o.qty,5)
		,i.nwht * CASE WHEN i.nwun = 'KG' THEN 2.2 ELSE 1 END
		,i.mpck
		,(o.pricing->>'guidance_price')::NUMERIC
	    ,(o.pricing->>'floor'  )::numeric(15,5)        
	    ,(o.pricing->>'ceiling')::numeric(15,5)
	    ,(o.pricing->>'uselimits')::boolean
)
SELECT * FROM npgrp
-- ----sequenced for most recent price------------
-- ,nseq AS (
-- 	SELECT 
-- 		 bill_dba 
-- 		,ship_dba
-- 		,part
-- 		,odate
-- 		,qtyord
-- 		,lbs
-- 		,pltq
-- 		,price
-- 		,guidance
-- 	    ,floor        
-- 	    ,ceiling   
-- 	    ,uselimits
-- 	    ,row_number() OVER (PARTITION BY bill_dba, ship_dba, part ORDER BY odate desc) rn
-- 	    ,round(avg(price) over (PARTITION BY bill_dba, ship_dba, part),5) avgprice
-- 	FROM 
-- 		npgrp
-- )
-- -- SELECT * FROM seq WHERE rn = 1
-- ,ngrp AS (
--     SELECT 
--         o.account
--         ,o.shipgrp
--         ,o.chan
--         ,o.dsm
--         ,i.partgroup
--         ,i.pricegroup
--         ,o.part
--         ,o.glec
--         ,o.oseas
--         -- ,o.odate
--         ,og.sortmo omon
--         ,o.sseas
--         -- ,o.sdate
--         ,sg.sortmo smon
--         -- ,o.calc_status
--         ,ROUND(sum(fb_qty * lbs),5) lbs
--         ,ROUND(sum(fb_qty),2) qty 
--         ,ROUND(sum(fb_val_loc * r_rate),2) sales_usd
--         ,ROUND(sum(fb_cst_loc_cur * c_rate),2) cost_curstd_usd
--     FROM 
--         profile o
--         LEFT OUTER JOIN rlarp.gld og ON 
--             o.odate BETWEEN og.sdat and og.edat
--         LEFT OUTER JOIN rlarp.gld sg ON
--             o.sdate BETWEEN sg.sdat and sg.edat
--         LEFT OUTER JOIN "CMS.CUSLG".itemm i ON 
--             i.item = o.part
--     WHERE
--         true
--         -- iter IN ('actuals','actuals_plug','copy')
--         AND fs_line = '41010' 
--         AND substring(o.glec,1,1) <= '2'
--         AND o.calc_status <> 'CANCELED'
--         AND o.sseas IS NOT NULL
--     GROUP BY 
--         o.account
--         ,o.shipgrp
--         ,o.chan
--         ,o.dsm
--         ,i.partgroup
--         ,i.pricegroup
--         ,o.part
--         ,o.glec
--         ,o.oseas
--         -- ,o.odate 
--         ,og.sortmo
--         ,o.sseas
--         -- ,o.sdate
--         ,sg.sortmo
--         -- ,o.calc_status
-- )
-- -- SELECT * FROM grp
-- ,nguid AS (
--     SELECT
--          grp.account
--         ,grp.shipgrp
--         ,grp.chan
--         ,r.repp dsm
--         ,r.director
--         ,grp.shipgrp||'.'||r.repp lu
--         -- ,grp.partgroup
--         -- ,grp.pricegroup
--         ,grp.part
--         -- ,grp.glec
--         ,2026  oseas
--         ,grp.omon 
--         -- ,grp.odate 
--         ,(2026- grp.oseas) + grp.sseas sseas
--         ,grp.smon
--         -- ,grp.sdate
--         ,grp.lbs
--         ,grp.qty 
--         ,grp.sales_usd
--         ,CASE WHEN round(grp.sales_usd/grp.qty,5) = 0 THEN null ELSE round(grp.sales_usd/grp.qty,5) END hist_price
--         ,grp.cost_curstd_usd
--         ,sum(grp.lbs) OVER (PARTITION BY shipgrp,  dsm) shiplbs
--         ,CASE WHEN seq.floor = 0 THEN null else seq.floor END floor
--         ,CASE WHEN seq.ceiling = 0 THEN null else seq.ceiling END ceiling
--         ,seq.uselimits
--         ,seq.price 
--         ,seq.avgprice
--         ,seq.pltq
--         ,ROUND(CASE WHEN seq.uselimits THEN 
--             CASE 
--                 WHEN glec = '1NU' THEN
--                     --if more than 8/24 pallets, use floor
--                     CASE WHEN grp.qty >= 24*seq.pltq THEN seq.floor
--                     -- if more than a pallet use the target price 
--                     ELSE CASE WHEN qtyord >=  8*seq.pltq THEN (seq.ceiling + seq.floor) / 2
--                     -- if more than a pallet use the target price 
--                     ELSE CASE WHEN qtyord >=  2*seq.pltq THEN seq.ceiling
--                     -- if more than a pallet use the target price 
--                     ELSE  seq.ceiling * 1.05
--                     END END END
--                 ELSE
--                     CASE WHEN pricegroup ~ '(Hanger|Dish)' THEN
--                         --at least 1 pallets is lower limit
--                         CASE WHEN qtyord >= 01*seq.pltq THEN seq.floor
--                         --at least 2 pallets is mid range
--                         ELSE CASE WHEN qtyord >=  0.5*seq.pltq THEN (seq.ceiling + seq.floor) / 2
--                         --less than a pallet is upper + 15%
--                         ELSE  seq.ceiling 
--                         END END
--                     ELSE 
--                         --at least 8 pallets is lower limit
--                         CASE WHEN qtyord >= 08*seq.pltq THEN seq.floor
--                         --at least 2 pallets is mid range
--                         ELSE CASE WHEN qtyord >=  2*seq.pltq THEN (seq.ceiling + seq.floor) / 2
--                         --at least 1 pallet is upper range
--                         ELSE CASE WHEN qtyord >=  1*seq.pltq THEN seq.ceiling
--                         --less than a pallet is upper + 15%
--                         ELSE  seq.ceiling * 1.15
--                         END END END
--                     END
--             END
--         ELSE 
--             null 
--         END,5) guid
--     FROM 
--         ngrp grp
--         LEFT OUTER JOIN nseq seq ON 
--             seq.ship_dba = grp.shipgrp
--             AND seq.bill_dba = grp.account
--             AND seq.part = grp.part
--             AND seq.rn = 1
--         LEFT OUTER JOIN rlarp.repc r ON 
--             r.rcode = grp.dsm
-- )
-- ,orphans AS (
--     SELECT
--          account
--         ,shipgrp
--         ,chan
--         ,dsm
--         ,director
--         ,lu
--         --,partgroup
--         --,pricegroup
--         ,part
--         -- ,glec
--         ,oseas
--         ,omon
--         -- ,odate 
--         ,sseas
--         ,smon
--         -- ,sdate
--         ,lbs
--         ,qty 
--         ,sales_usd
--         ,hist_price
--         ,cost_curstd_usd
--         ,shiplbs
--         ,floor
--         ,ceiling
--         ,uselimits
--         ,price 
--         ,avgprice
--         ,pltq
--         ,guid
--         ,least(hist_price, price, avgprice) lowest_last
--         ,least(hist_price, price, avgprice, floor) lowest_floor
--     FROM 
--         nguid
-- )
