从另一个表更新表,选择最近的'日期'对于每个供应商

时间:2012-09-03 09:05:34

标签: sql sql-server

我正在使用SupplierA中的详细信息更新SupplierB

SupplierB包含供应商提供的每个订单,但是,当我插入SupplierA时,我只想为每个供应商添加一个实例,因此 most recent 与订单相关的日期。

目前我正在做这样的事情;

UPDATE SupplierA
SET LastSupplyDate = (SELECT [DATE] FROM SupplierB AS SB WHERE SB.Supplier COLLATE DATABASE_DEFAULT = SupplierA.SupNumber COLLATE DATABASE_DEFAULT)
WHERE [Site] = 2

显然这会返回多个值,如何获得最新值,MAX()是一个选项?我很欣赏任何想法/想法吗?

3 个答案:

答案 0 :(得分:3)

SET LastSupplyDate = (SELECT MAX([DATE])...

答案 1 :(得分:1)

UPDATE SupplierA SET LastSupplyDate =   
(  
  SELECT TOP 1 [DATE] FROM SupplierB AS SB  
  WHERE SB.Supplier
  COLLATE DATABASE_DEFAULT = SupplierA.SupNumber COLLATE DATABASE_DEFAULT 
  ORDER BY [DATE] DESC


) 
WHERE [Site] = 2

答案 2 :(得分:1)

UPDATE SupplierA
SET LastSupplyDate = (SELECT MAX([DATE]) FROM SupplierB AS SB WHERE SB.Supplier COLLATE DATABASE_DEFAULT = SupplierA.SupNumber COLLATE DATABASE_DEFAULT)
WHERE [Site] = 2