Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alternative to AND/OR? 2

Status
Not open for further replies.

FederalProgrammer

Programmer
Jul 2, 2003
318
CA
Consider the following data; I need a query that will return ID = 1 only if anotherGroupID = {1, 2, 3}.
Code:
id  |  anotherGroupID
----------------------
 1  |   1
----------------------
 1  |   2
----------------------
 1  |   3
----------------------
 2  |   1
----------------------
 2  |   5
----------------------
 2  |   3
----------------------

I've tried:
Code:
 Select ID from table where AnotherGroupID = 1 Or 2 Or 3
But this returns 1 and 2!
I've also tried
Code:
SELECT 
anotherGroupID,
ID

FROM 
table

GROUP BY 
anotherGroupID, ID

Having
anotherGroupID = 1 OR
anotherGroupID = 2 OR
anotherGroupID = 3

ANY IDEAS?
 
Select ID from table where AnotherGroupID IN (1, 2, 3)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

Select ID
from table
where id=1 AND AnotherGroupID IN (1, 2, 3)

 
hmmm, thanx.
But you see, the whole point of the query is to return the id of the group (1, 2, 3).
You are assuming that ID is known, whereas, I'm trying to find out what ID is.
The query should be such that, given a list of "anotherGroupID", the associated "ID" can be determined.

Query for "anotherGroupID" = 1, 2, 3 should return 1 (and only 1).
Query for "anotherGroupID" = 1, 5, 3 should return 2 (and only 2).
and so on. The point is, the result of the query will determine what the corresponding id is.

 
Select ID from table where AnotherGroupID IN (1, 5, 3)
GROUP BY ID HAVING COUNT(*)=3

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
hmm
but the problem is the count of anotherGroupID is not always 3...

May the following dataset would be a better example:

Code:
id  |  anotherGroupID
----------------------
 1  |   1
----------------------
 1  |   2
----------------------
 1  |   3
----------------------
 2  |   2
----------------------
 2  |   7
----------------------
 2  |   9
----------------------
 2  |   11
----------------------
 3  |   1
----------------------
 4  |   2
----------------------
 4  |   534
----------------------
 
May the following dataset would be a better example
And what is the expected result ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

So, continuing with PHV's answer:

Select ID from table where AnotherGroupID IN (1, 5, 3)
GROUP BY ID HAVING COUNT(*)=3

Select ID from table where AnotherGroupID IN (2, 7, 9, 11)
GROUP BY ID HAVING COUNT(*)=4

Select ID from table where AnotherGroupID IN (2, 7, 9)
GROUP BY ID HAVING COUNT(*)=3

Select ID from table where AnotherGroupID IN (7, 9, 11)
GROUP BY ID HAVING COUNT(*)=3

Select ID from table where AnotherGroupID IN (2, 11)
GROUP BY ID HAVING COUNT(*)=2

Select ID from table where AnotherGroupID IN (2, 534)
GROUP BY ID HAVING COUNT(*)=2

 
Thanx for the replies. But I'm gonna try to be more clear:

I need a query like the following:
Code:
Select ID
From Table
Where 
anotherGroupID = $A_Set_of_anotherGroupID

think of $A_Set_of_anotherGroupID as a paramater; It can be anything: {1 2 3} or {53, 44, 39}.
$A_Set_of_anotherGroupID = {1 2 3} -> 1
$A_Set_of_anotherGroupID = {2 534} -> 4
$A_Set_of_anotherGroupID = {1 2 534} -> NO RESULTS
$A_Set_of_anotherGroupID = {1 2 3 534} -> NO RESULTS

Also, to be more clear, $A_Set_of_anotherGroupID, is not really a parameter to the query. I am writing this query in .NET; so obviously I can create the condition accordingly.

I hope now you see that, COUNT cannot be usefull (because it is impossible to tell what COUNT(*) should be equal to!)



---------------
 
Thanx for the replies. But I'm gonna try to be more clear:

I need a query like the following:
Code:
Select ID
From Table
Where 
anotherGroupID = $A_Set_of_anotherGroupID

think of $A_Set_of_anotherGroupID as a paramater; It can be anything: {1 2 3} or {53, 44, 39}.
$A_Set_of_anotherGroupID = {1 2 3} -> 1
$A_Set_of_anotherGroupID = {2 534} -> 4
$A_Set_of_anotherGroupID = {1 2 534} -> NO RESULTS
$A_Set_of_anotherGroupID = {1 2 3 534} -> NO RESULTS

Also, to be more clear, $A_Set_of_anotherGroupID, is not really a parameter to the query. I am writing this query in .NET; so obviously I can create the condition accordingly.

I hope now you see that, COUNT cannot be usefull (because it is impossible to tell what COUNT(*) should be equal to!)
 
So, in .NET you can't count the number of values in $A_Set_of_anotherGroupID ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
hehehe, I like your sarcasm. Of course i can :)
but consider the following case:

Code:
id  |  anotherGroupID
----------------------
 1  |   1
----------------------
 1  |   2
----------------------
 2  |   1
----------------------
 2  |   3
----------------------
 3  |   1
----------------------
 3  |   7
----------------------

do you think the count can help me here? :)
 
I really don't see where is the new problem in your last sample data.
Provided your .NET code build on the fly the SQL code, you simply have to do something like this:
SELECT ID FROM table WHERE AnotherGroupID IN (comma separated list of the values in $A_Set_of_anotherGroupID)
GROUP BY ID HAVING COUNT(*)=number of values in $A_Set_of_anotherGroupID


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
well, if you run the following query
Select ID from table where AnotherGroupID IN (1, 3)
GROUP BY ID HAVING COUNT(*)=2

will return
ID = 1, 2 and 3;

The correct result however, should be 2.
 
And which RDBMS returns such false result ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
If ID shouldn't have anotherGroupID outside the list:
SELECT ID FROM table G
WHERE AnotherGroupID IN (comma separated list of the values in $A_Set_of_anotherGroupID)
AND number of values in $A_Set_of_anotherGroupID=(SELECT Count(*) FROM table WHERE ID=G.ID)
GROUP BY ID HAVING Count(*)=number of values in $A_Set_of_anotherGroupID

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
it sounds like we're almost there...
I just have difficulty understanding the following:

Code:
number of values in $A_Set_of_anotherGroupID

where is this field... Please don't worry about my .net code. I'll translate it to code later. I really think you're onto something... How would this number of values in $A_Set_of_anotherGroupID be done in sql?



---------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top