Skip to content
Advertisement

How to exclude certain values from a Query [SQL]

as the title states, an NotPrimID, can have two different values Example 1 and Example 2

"ExampleTable"

NotPrimID     Text
0             "Example 1"
0             "Example 1"
0             "Example 2"
1             "Example 1"
1             "Example 1"
2             "Example 1"
2             "Example 2"

IF: if NotPrimID does have both values, it should be automatically excluded from the query result.

What i want: Query, that will deliever all the NotPrimID, that only have “Example 1” as a result, however if NotPrimID does have “Example 2” also as a possible result, it should be automatically excluded.

Question: How to exclude certain NotPrimID, if it does have additional values ?

Advertisement

Answer

You can use group by and having:

select notprimid
from exampletable
group by notprimid
having min(text) = max(text) and min(text) = 'Example1'

This returns all notprimids that have only one distinct text, whose value must be 'Example1'.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement