Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

Localizando duplicatas em uma coluna usando select where no SQL Server 2008

SELECT Id, Terms, Track, Active
FROM QueryData
WHERE Terms IN (
                SELECT Terms 
                FROM QueryData
                WHERE Track = 'Y' and Active = 'Y' 
                GROUP BY Terms
                HAVING COUNT(*) > 1
                )

Demonstração do SQLFiddle

Dados:
ID      Terms     Track    Active
100     paper     Y        Y
200     paper     Y        Y
100     juice     Y        Y
400     orange    N        N
1000    apple     Y        N

Resultados:
Id      Terms     Track    Active
100     paper     Y        Y
200     paper     Y        Y