1

更新:它现在可以正确提取每件商品最近交付的供应商名称。我现在遇到的问题是它没有提取以前没有交付的记录(空白 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
4

1 回答 1

2

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

使用此 CTE,您可以按某些标准(即您的)对您的数据进行分区,Item并让 SQL Server 为每个“分区”从 1 开始为您的所有行编号,并按某些标准排序。

所以尝试这样的事情:

;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) 

变成NULL9999 年 12 月 31 日的日期 - 当按降序排序时,这些日期总是排在第一位。

于 2013-01-16T20:28:20.157 回答