Martin Gemme
Martin Gemme

Reputation: 345

(SQL-T-SQL) Select Command to Have only certain values?

Here is my table app_extra:

AppID;AppExtraID
100;0
100;1
100;3
100;7
100;8
100;9
110;0
110;2
110;4
110;7
110;9
115;0
115;2
115;6
115;8
120;0
120;1
120;10
130;0
130;7
130;8
130;10
140;0
140;1
140;3
150;0
150;2
150;6
150;7
150;8
150;10
160;0
160;8
160;10
165;0
165;8
165;10
170;0
170;2
170;8
170;10
180;0
180;1
180;5
180;7
180;10
185;0
185;1
185;7
185;10
190;0
190;2

I would like to know how to have only the AppID that doesn't have 9 and 10 AppExtraID

Thanks!

Upvotes: 0

Views: 499

Answers (4)

Tevo D
Tevo D

Reputation: 3381

I see what you are getting at...

SELECT DISTINCT AppID
FROM app_extra 
WHERE AppID NOT IN 
  (SELECT DISTINCT AppID from app_Extra 
   WHERE AppExtraID IN (9, 10))

The inner select will identify all App ID's which have a 9 or a 10 extra. These rows will then be excluded in the main select. Any AppID which is related to 9 or 10 will be eliminated, even if they also have another extra ID.

Upvotes: 2

Erwin Brandstetter
Erwin Brandstetter

Reputation: 659317

To get a unique list of AppID that fulfill the reqirement:

SELECT AppID 
FROM   tbl
WHERE  AppExtraID NOT IN (9, 10)
GROUP  BY 1

Upvotes: 1

UnhandledExcepSean
UnhandledExcepSean

Reputation: 12804

SELECT DISTINCT AppID FROM app_extra WHERE AppExtraID NOT IN(9,10)

Upvotes: 0

user596075
user596075

Reputation:

select distinct AppId
from app_extra
where AppExtraID not in (9, 10)

Modified to suit your comment's desire.

Upvotes: 2

Related Questions