sql max last\ U update列

doinxwow  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(425)

我正在寻找以下更新查询的帮助。我正在尝试更新 provreg 表的最新更新的契约,但它似乎不正确。使用contractinfo表上的max(lastupdate)字段完成此更新的最佳方法是什么?

UPDATE provreg
SET ContractName = ci.ContractID,
    ContractBegin = ci.ContractEff,
    ContractEnd = ci.contractterm
FROM provreg pr
INNER JOIN Provider P ON pr.npi = p.npi
INNER JOIN Affiliation A ON a.provid = p.provid
INNER JOIN contractinfo ci ON ci.affiliationid = a.affiliationid
WHERE CI.CONTRACTED = 'Y'
  AND ci.lastupdate = (SELECT MAX(lastupdate)
                       FROM PlanReport_QNXT_LA.dbo.contractinfo 
                       WHERE contractid = ci.contractid)
agyaoht7

agyaoht71#

试试下面。

UPDATE PROVREG
   SET (CONTRACTNAME, CONTRACTBEGIN, CONTRACTEND) =
          (SELECT CI.CONTRACTID, CI.CONTRACTEFF, CI.CONTRACTTERM
             FROM PROVREG PR
                  INNER JOIN PROVIDER P ON PR.NPI = P.NPI
                  INNER JOIN AFFILIATION A ON A.PROVID = P.PROVID
                  INNER JOIN CONTRACTINFO CI
                     ON CI.AFFILIATIONID = A.AFFILIATIONID
            WHERE     CI.CONTRACTED = 'Y'
                  AND CI.LASTUPDATE = (SELECT MAX (LASTUPDATE)
                                         FROM PLANREPORT_QNXT_LA.DBO.CONTRACTINFO
                                        WHERE CONTRACTID = CI.CONTRACTID));

COMMIT;

相关问题