UPDATE: It’s now correctly pulling the supplier names for the most recent delivery for each item. The issue I’m having now is that it’s not pulling records that don’t have previous deliveries (blank dlvry_dt). Does anyone know how to fix this?
Thanks a bunch for your help so far!
;WITH Items As(
Select
a.bin, a.item, d.item_description As 'Vintage', a.min_reorder,
c.minor_item_class, c.minor_class_description, a.qty_available,
a.reorder_threshold As 'Par', a.avg_unit_cost,
a.qty_available*a.avg_unit_cost As 'Valuation', e.dlvry_dt, g.supplier,
g.supplier_name,
RowNum = ROW_NUMBER() OVER(PARTITION BY a.item ORDER BY dlvry_dt DESC)
from argus.STORE_INVENTORY a, argus.MAJOR_ITEM_CLASS b,
argus.MINOR_ITEM_CLASS c, argus.ITEM_MASTER d, argus.DELIVERY e
inner join argus.delivery_line_item f
on e.delivery = f.delivery
and e.purchase_order = f.purchase_order
and e.customer = f.customer
and e.store = f.store
and e.supplier = f.supplier
full outer join argus.supplier g
on e.supplier = g.supplier
where a.customer = 10005
and a.store = 1
and d.item = a.item
and b.major_item_class = c.major_item_class
and d.minor_item_class = c.minor_item_class
and (b.major_item_class = 15 or b.major_item_class = 17 or b.major_item_class = 14)
and (c.minor_item_class = 830 or c.minor_item_class = 175 or c.minor_item_class = 880 or c.minor_item_class = 661 or c.minor_item_class = 651 or c.minor_item_class = 785 or c.minor_item_class = 716 or c.minor_item_class = 810 or c.minor_item_class = 850 or c.minor_item_class = 885 or c.minor_item_class = 998 or c.minor_item_class = 840 or c.minor_item_class = 855 or c.minor_item_class = 280)
and f.line_item_status <> 'D'
and e.customer = 10005
and e.store = 1
and f.item = a.item
--and (c.minor_item_class = 176 or c.minor_item_class = 651 or c.minor_item_class = 661 or c.minor_item_class = 716 or c.minor_item_class = 810 or c.minor_item_class = 830 or c.minor_item_class = 840 or c.minor_item_class = 850 or c.minor_item_class = 855 or c.minor_item_class = 885 or c.minor_item_class = 998)
Group By c.minor_class_description, a.bin, a.item, d.item_description, a.min_reorder, a.qty_available, a.reorder_threshold, a.avg_unit_cost, c.minor_item_class, e.dlvry_dt, g.supplier, g.supplier_name
)
Select bin, item, Vintage, min_reorder, minor_item_class, minor_class_description, qty_available, Par, avg_unit_cost, Valuation, dlvry_dt, supplier, supplier_name
From Items
Where RowNum =1
Order By minor_class_description
One approach would be to use a CTE (Common Table Expression).
With this CTE, you can partition your data by some criteria – i.e. your
Item– and have SQL Server number all your rows starting at 1 for each of those “partitions”, ordered by some criteria.So try something like this:
Here, I am selecting only the “first” entry for each “partition” (i.e. for each
Item) – ordered by the descendingDeliveryDate.Does that approach what you’re looking for??
Update: if you want to include possible
NULLentries inDeliveryDate, too, you could use something liketo turn
NULLinto dates of 31-Dec-9999 – those will always come first when ordered in a descending order.