SQL Only Pull Record包含最近的日期

时间:2013-01-16 20:19:49

标签: sql sql-server

更新:现在正确地为每个项目提取最近交货的供应商名称。我现在遇到的问题是它没有提取没有先前交付的记录(空白dlvry_dt)。有谁知道如何解决这一问题?

到目前为止,非常感谢你的帮助!

;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

1 个答案:

答案 0 :(得分:2)

一种方法是使用CTE(公用表表达式)。

使用此CTE,您可以按照某些条件对数据进行分区 - 即Item - 并为每个“分区”提供从1开始的所有行的SQL Server编号,按某些条件排序。

所以尝试这样的事情:

;WITH Items AS
(
   SELECT 
       Item, Vintage, Qty, DeliveryDate,
       RowNum = ROW_NUMBER() OVER(PARTITION BY Item ORDER BY DeliveryDate DESC) 
   FROM 
       dbo.YourTableHere   -- possibly several JOINs
   WHERE
      ......
)
SELECT 
   Item, Vintage, Qty, DeliveryDate
FROM 
   Items
WHERE
   RowNum = 1

在这里,我只选择每个“分区”的“第一个”条目(即每个Item) - 按降序DeliveryDate排序。

这会接近你想要的吗?

更新:如果您想在NULL中添加可能的DeliveryDate条目,也可以使用类似

的内容
       RowNum = ROW_NUMBER() OVER(PARTITION BY Item ORDER BY ISNULL(DeliveryDate, '99991231' DESC) 

NULL转换为1999年12月31日的日期 - 按降序排序时,这些日期将始终排在第一位。