Skip to content
Advertisement

How to pick data of the same IDs without repetition

How do I pick data of same IDs without any repetition?

DECLARE @YourTable TABLE (ID INT, NAME VARCHAR(100))

INSERT INTO @YourTable
SELECT 123, 'James Hill'

INSERT INTO @YourTable
SELECT 123, 'Hill, James'

INSERT INTO @YourTable
SELECT 234, 'Doe, Mary Jane'

INSERT INTO @YourTable
SELECT 234, 'Doe, Mary Jane D.'

INSERT INTO @YourTable
SELECT 234, 'Doe, Mary Jane'

FROM @yourTable
WHERE cast_id IN (
    SELECT Name
    FROM @yorTable
    GROUP BY ID
    HAVING COUNT(*) > 1
)";

What is a way to handle my data?

Advertisement

Answer

If any of the names from the redundant names is acceptable, you can achieve your desired results by the following script:

SELECT ID,MAX(Name)
FROM @YourTable
GROUP BY ID
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement