WITH
----grouped prices-------------
pgrp AS (
	SELECT
		o.bill_dba 
		,o.ship_dba
        ,o.dsm
		,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
	WHERE 
		VERSION = 'Actual' 
		AND oseas >= 2024
		AND calc_status <> 'CANCELED' 
		AND o.fs_line = '41010'
		AND o.part <> ''
		AND substring(o.glec,1,1) <= '2'
		AND o.qty <> 0
	GROUP BY 
		o.bill_dba 
		,o.ship_dba
        ,o.dsm
		,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
)
----sequenced for most recent price------------
,seq 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 
		pgrp
)
-- SELECT * FROM seq WHERE rn = 1
,grp AS (
    SELECT 
        o.account
        ,o.shipgrp
        ,o.dsm
        ,o.part
        ,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 
        rlarp.osmf 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
    WHERE
        iter IN ('actuals','actuals_plug','copy')
    GROUP BY 
        o.account
        ,o.shipgrp
        ,o.dsm
        ,o.part
        ,o.oseas
        -- ,o.odate 
        ,og.sortmo
        ,o.sseas
        -- ,o.sdate
        ,sg.sortmo
        -- ,o.calc_status
)
    SELECT
         grp.account
        ,grp.shipgrp
        ,grp.dsm
        ,grp.part
        ,grp.oseas
        ,grp.omon
        -- ,grp.odate 
        ,grp.sseas
        ,grp.smon
        -- ,grp.sdate
        ,grp.lbs
        ,grp.qty 
        ,grp.sales_usd
        ,grp.cost_curstd_usd
        ,sum(grp.lbs) OVER (PARTITION BY shipgrp, oseas) shiplbs
        ,seq.floor
        ,seq.ceiling
        ,seq.uselimits
        ,seq.price 
        ,seq.avgprice
        ,seq.pltq
    FROM 
        grp
        LEFT OUTER JOIN seq ON 
            seq.ship_dba = grp.shipgrp
            AND seq.bill_dba = grp.account
            AND seq.part = grp.part
            AND seq.rn = 1
