请帮助我修复以下查询,它应该通过对字段 MEMBID、FSTNAM、LSTNAM、PRVID 进行分组来更新重复记录,而不是所有记录。
UPDATE RL4CLT.Duplicaterecords
SET ERRSTS = 'B' -- Set to 'B' for duplicates
WHERE TRNID = 650500 -- Specify the TRNID for filtering
AND (MEMBID, FSTNAM, LSTNAM, PRVID) IN (
SELECT MEMBID, FSTNAM, LSTNAM, PRVID
FROM RL4CLT.Duplicaterecords
WHERE TRNID = 650500 -- Ensure you are checking within the same TRNID
GROUP BY MEMBID, FSTNAM, LSTNAM, PRVID
HAVING COUNT(*) > 1 -- Only include those with duplicates
);
我尝试使用 Row_num 并使用另一个子查询,但没有成功。它只需要更新上述指定字段中具有匹配值的记录。
尝试使用 ROW_NUMBER() 为由(MEMBID、FSTNAM、LSTNAM、PRVID)定义的组中的每一行分配行号,按这些列分区并按唯一列排序(将其更改为适合您需要的列) ):
UPDATE RL4CLT.Duplicaterecords
SET ERRSTS = 'B'
WHERE TRNID = 650500
AND (MEMBID, FSTNAM, LSTNAM, PRVID) IN (
SELECT MEMBID, FSTNAM, LSTNAM, PRVID
FROM (
SELECT MEMBID, FSTNAM, LSTNAM, PRVID,
ROW_NUMBER() OVER (PARTITION BY MEMBID, FSTNAM, LSTNAM, PRVID ORDER BY SOME_UNIQUE_COLUMN) AS RN
FROM RL4CLT.Duplicaterecords
WHERE TRNID = 650500
) AS Subquery
WHERE RN > 1
)