我可以使用以下语句选择要更新的行的信息:
select max(Date),F_ID from MyTable group by F_ID
F_ID 是外键。我正在尝试更新日期最大的不同字段。问题是可能有许多行具有相同的外键和相同的日期。如果这些是最大日期有多行,我想更新所有这些。
我能得到的最接近的是:
update MyTable set
Curr = 1
where (select cast(Date as varchar(20))+cast(F_ID as varchar(10)) from MyTable) in
(select cast(max(Date)as varchar(20))+cast(F_ID as varchar(10)) from MyTable group by A_PID)
我收到此错误:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
谢谢您的帮助!
编辑:更新语句的最终查询格式,以防其他人正在寻找这个:
update MyTable set
Curr = 1
from MyTable t inner join
(select F_ID,max(Date) maxDate
from MyTable group by A_PID) t2
on t.A_PID = t2.A_PID and t.Date = t2.maxDate
感谢@Jim