I've got a big query that's returning rows where one of the colums has duplicated values, for example:
| sysid | col1 | col2
| A | 1 | 2
| A | 2 | 3
| B | 1 | 4
sysid is the column I want to filter by, so in the above example I only want the last row of output because A appears more than once. I tried to do this to filter them out:
CREATE TABLE #temp (SysId VARCHAR(10), col1 INT);
INSERT INTO #temp VALUES ('A', 1), ('B', 1), ('A', 1);
WITH cte AS (
SELECT * FROM #temp
), sysids AS (
SELECT SysId FROM #temp GROUP BY Sysid HAVING COUNT(*) = 1
)
SELECT * FROM #temp WHERE EXISTS (SELECT SysId FROM sysids);
DROP TABLE #temp;
I expected that final select would only contain the B row, in the above example . I'm still getting all of them though, and I don't understand why.
Obviously in this specific example I don't need the cte part but my real query is pretty complex with multiple unions.