我有一个
tableD (ID int, matchID char, PID char, set int, num char, status char, msg char)
ID matchID PID set num status msg
1 00A123 xx123 1 11
2 00B247 xx213 1 13
3 00H417 xx317 2 15
4 00G547 xx328 3 16
5 00Y547 xx721 3 19
TableP
PID num
xx213 13
...................
现在我们必须将status = void
设置在同一个集合中,
对于PID
和tabled
中的同一tableP
,tableP
中不存在不同的num或记录。如果[num]
中至少存在一个对应的tableP
,则适用此选项。保持一个匹配并且对所有其他人进行VOID。同样在[msg]
中,它应该说__是匹配的[ID]。
如果同一组中没有匹配,请传递具有最低[matchID]
的那个,并使用相同的msg使所有其他组无效。另外[matchID]
始终为00*xxxxxxx (10 digit)
* is alphabet
和x is (0-9)
。
预期结果:
ID matchID PID set num status msg
1 00A123 xx123 1 11 VOID <duplicate of ID = 2>
2 00B247 xx213 1 13 NULL NULL
3 00H417 xx317 2 15 NULL NULL
4 00G547 xx328 3 16 NULL NULL (G<Y)
5 00Y547 xx721 3 19 VOID <duplicate of ID = 4>
我试过了:
;WITH setInfo AS (
SELECT [SET],
CASE WHEN EXISTS (SELECT 1 FROM tableD b
WHERE b.[set]=a.[set] AND num IS NOT NULL)
THEN 1 ELSE 0 END AS HasNum,
MIN(matchID) AS MinMatchPID
FROM tableD a
GROUP BY [SET]
)
UPDATE a SET
PID = CASE WHEN s.HasNum = 0 AND a.PID = s.MinMatchPID THEN a.ID
WHEN s.HasNum = 1 AND a.PID = (SELECT min(MatchID) FROM tableD b
WHERE b.[set] = a.[set]
AND b.num is not null) THEN a.PID
END
FROM TableD a
JOIN setInfo s ON s.[set] = a.[set]
;WITH CTE_Duplicates1 AS
(SELECT MIN(ID) OVER (PARTITION BY [set]) Mn1,
ROW_NUMBER() OVER (PARTITION BY [set]
ORDER BY ID) RN,*
FROM tableD )
UPDATE CTE_Duplicates1
SET status = 'VOID',
Message = '<V-Duplicate of ID=' + CAST(Mn1 as VARCHAR(15))+'>'
WHERE RN>1
没有给出准确的结果。