聚合可能不会出现在UPDATE语句的集合列表中(UPDATE)

时间:2018-03-20 13:10:43

标签: sql-server

我怀疑这个问题与MIN语句有关,但不确定如何修复它:

UPDATE t1

SET t1.PrintDate = MIN(t2.[Print Date])

FROM [Data Checking].[dbo].[MonthlyReads] t1

INNER JOIN [Data Checking].[dbo].[BILLWINDOWS] t2
ON t1.MRU = t2.[MR Unit]

WHERE t2.[Print Date] > t1.[ReadingDate]

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

你可以这样做:

;WITH t2 ([MR Unit] , [Print Date]) as 
(
    SELECT [MR Unit] , MIN([Print Date])
    FROM [Data Checking].[dbo].[BILLWINDOWS]
    GROUP BY [MR Unit] 
)

UPDATE t1
SET t1.PrintDate = t2.[Print Date]
FROM [Data Checking].[dbo].[MonthlyReads] t1
INNER JOIN t2
ON t1.MRU = t2.[MR Unit]
WHERE t2.[Print Date] > t1.[ReadingDate]