Need help with a SQL query that returns the lasts records matching a certain criteria
我有一个类似于引用帖子中的查询。我正在尝试使用它将另一个字段设置为1,仅用于返回组和MAX结果的情况。所以我试过这个:
Update FilesTransmitted
set Accepted = 1
select MAX(f.UID) as UID, f.DocumentNumber, f.REV
from FilesTransmitted f
INNER JOIN FilesReturned r
on f.DocumentNumber = r.[Document #]
and f.REV = r.REV
where NOT(r.CODE IS NULL)
group by f.DocumentNumber, f.REV
order by f.DocumentNumber;
导致所有记录都设置了Accepted字段,而不仅仅是MAX UID记录。我知道我使用更新错误,但查询对我来说足够复杂,我只是不确定如何为更新部分构建它。
答案 0 :(得分:2)
尝试:
Update FilesTransmitted t
set Accepted = 1
where t.uid = (select max(f.UID)
from FilesTransmitted f
join FilesReturned r
on f.DocumentNumber = r.[Document #]
and f.REV = r.REV
where r.CODE is null
and f.DocumentNumber = t.DocumentNumber
and f.rev = t.rev
group by f.DocumentNumber, f.REV);
答案 1 :(得分:0)
你写的内容基本上是2个查询,添加一个空白行,你会看到我的意思:
Update FilesTransmitted
set Accepted = 1
select MAX(f.UID) as UID, f.DocumentNumber, f.REV
from FilesTransmitted f
INNER JOIN FilesReturned r
on f.DocumentNumber = r.[Document #]
and f.REV = r.REV
where NOT(r.CODE IS NULL)
group by f.DocumentNumber, f.REV
order by f.DocumentNumber;
您需要的是更新中的WHERE子句:
Update FilesTransmitted
set Accepted = 1
WHERE f.UID = ...