문제

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

도움이 되었습니까?

해결책

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

다른 팁

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
)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top