SQL Server Group By在UPDATE语句中

时间:2015-08-27 14:25:26

标签: sql sql-server

如何在此声明中使用GROUP BY

UPDATE LoanMaster 
SET LeadsID1 = NEXT VALUE FOR LM
WHERE PrdAcctId IS NOT NULL
GROUP BY LBrCode, CustNo

1 个答案:

答案 0 :(得分:0)

我将推测您需要LBrCodeCustNo对的唯一ID。你可以这样做:

with nums as (
      SELECT t.*, (NEXT VALUE FOR LM) as newval
      FROM (SELECT DISTINCT LBrCode, CustNo
            FROM LoanMaster
            WHERE PrdAcctId IS NOT NULL
           ) t
     )
update lm
    set LeadsId1 = newval
    from LoanMaster lm JOIN
         nums
         on lm.LBrCode = nums.LBrCode and lm.CustNo = nums.CustNo;

注意:尽管这应该有效,但您应该创建一个Leads表,每个值一行。看起来你想要一个外键关系,你应该有一个关系的实体。