带连接的SQL Server更新

时间:2012-01-23 14:08:57

标签: sql

我正在尝试根据另一个表格中的date更新表格中的MAX(date)。为了获得正确的数据链接,我必须做2个内连接和2个左外连接。

我可以选择正确的数据,它返回一个Guid(PersonId)和Date。

我必须使用此信息来更新我的原始表。我无法使其工作,我仍然遇到语法错误。

update tblqualityassignments as assign
inner join tblrequirementteams as team on assign.guidmemberid = team.guidmemberid
set assign.dtmQAPCLed = dtmTaken
from
(
   select reg.guidpersonid, max(certs.dtmTaken) as dtmTaken  from tblqualityassignments as assign
   inner join tblrequirementteams as team on assign.guidmemberid = team.guidmemberid
   inner join tblregisteredusercerts as reg on team.guidpersonid = reg.guidpersonid
   left outer join tblcerttaken as certs on certs.guidcertid = reg.guidcertid
   left outer join tblCodesCertType as types on types.intcerttypeid = certs.intcerttypeid
   where types.intcerttypeid = 1 
    and assign.guidmemberid = team.guidmemberid 
   group by reg.guidpersonid as data
)
where data.guidpersonid = team.guidpersonid

1 个答案:

答案 0 :(得分:2)

假设您正在使用SQL Server,那么这应该可行:

UPDATE A
SET A.dtmQAPCLed = dtmTaken
FROM tblqualityassignments AS A
INNER JOIN tblrequirementteams as T 
ON A.guidmemberid = T.guidmemberid
INNER JOIN (select reg.guidpersonid, max(certs.dtmTaken) as dtmTaken  
            from tblqualityassignments as assign
            inner join tblrequirementteams as team 
            on assign.guidmemberid = team.guidmemberid
            inner join tblregisteredusercerts as reg 
            on team.guidpersonid = reg.guidpersonid
            left outer join tblcerttaken as certs 
            on certs.guidcertid = reg.guidcertid
            left outer join tblCodesCertType as [types] 
            on [types].intcerttypeid = certs.intcerttypeid
            where [types].intcerttypeid = 1 
            and assign.guidmemberid = team.guidmemberid 
            group by reg.guidpersonid) data
ON T.guidpersonid = data.guidpersonid