Reputation: 5
I am trying to return the IDs in a table where the IDs do not contain a value in another column. There are multiple IDs in the table that are the same, however the second column contains a different value. I want to return all of the distinct IDs where the second column does not contain a value of 10.
For example my data table looks like:
ID | Value
32 | 8
32 | 9
32 | 10
35 | 8
35 | 9
As per the table above I want to return all the IDs that do not contain 10, however when I run the following script I am getting back a result that gives me all the values that contain 8 & 9 and only omits 10 when I want just the distinct ID that have no rows that all contain 10 in the Values column.
I am also joining to another table.
select distinct table1.ID, table2.Values
from table1
left join table2 on table1.ID=table2.ID
where table2.Values <> 10
Upvotes: 0
Views: 162
Reputation: 95554
One method would be a HAVING
:
SELECT ID
FROM dbo.YourTable
GROUP BY ID
HAVING COUNT(CASE [Value] WHEN 10 THEN 1 END) = 0;
Upvotes: 1