I want to know how to update values of a column where they are repeated.
Example I have this
ColumnA | ColumnB | ColumnC | Repeated? |
---|---|---|---|
NAMEA | 111 | 1234 | No |
NAMEC | 222 | 5678 | No |
NAMEB | 222 | 5678 | No |
And I need change column (Repeated?) to YES WHERE ColumnB and ColumnC are equals like this:
ColumnA | ColumnB | ColumnC | Repeated? |
---|---|---|---|
NAMEA | 111 | 1234 | No |
NAMEC | 222 | 5678 | Yes |
NAMEB | 222 | 5678 | Yes |
Its possible? Which script?
I tried
UPDATE [MY_TABLE]
SET [Repeated?] = 'Yes'
FROM [MY_TABLE] as A
INNER JOIN
(
SELECT ColumnA, ColumnC
FROM [MY_TABLE]
GROUP BY ColumnA, ColumnC
) as B
ON B.ColumnC = A.ColumnC, B.ColumnA = B.ColumnA
CodePudding user response:
I think this should do the trick
UPDATE yt1
SET Repeated= (CASE WHEN yt2.ColumnA IS NULL THEN 'No' ELSE 'Yes' END)
FROM YourTable yt1
LEFT OUTER JOIN YourTable yt2
ON yt1.ColumnB = yt2.ColumnB AND yt1.ColumnC = yt2.ColumnC
WHERE yt1.ColumnA <> yt2.ColumnA