Question

Tabls is

ID  Count
1    30
2    30
3    10
4    15
5    10
6    25

I want query which will give me

4    15
6    25

in result

Was it helpful?

Solution

You can use NOT EXISTS:

SELECT ID,  Count
FROM dbo.TableName t1
WHERE NOT EXISTS
(
   SELECT 1 FROM dbo.TableName t2
   WHERE t1.ID <> t2.ID AND t1.Count = t2.Count
)

Demo

OTHER TIPS

The following should select what you want:

SELECT t.ID, t.[Count] 
FROM Table t 
WHERE 
   (SELECT COUNT(*) FROM Table t1 WHERE t1.[Count] = t.[Count]) = 1

Please note that you should really have an index on Table.[Count].

you could also do it with a grouping statement

SELECT MIN(ID), Count
FROM Table
GROUP BY Count
HAVING COUNT(*) = 1

Use HAVING together with COUNT DISTINCT, to limit the result:

SELECT [Id], [Count]
FROM MyTable
WHERE [Count] IN (
    SELECT [Count]
    FROM MyTable
    GROUP BY [Count]
    HAVING COUNT(DISTINCT [Count]) = 1
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top