473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Select Unique ID with only a set of values

GTXY20
29
Hello all;

If I have the following data:

ID Value
1 A
1 B
2 A
2 B
2 C
3 A
3 B

How would I go about only selecting the ID's that have only have the combination of values A & B - so my select query woud return:

ID Value
1 A
1 B
3 A
3 B

ID 2 is not returned because it also has a value of C.

I have tried:

SELECT table.ID, table.Value,
FROM table
WHERE (((table.Value)="A")) AND (((table.value)="B"))

but no luck.

Thanks in advance!
Oct 10 '07 #1
3 1715
nico5038
3,080 Expert 2GB
Multiple row processing isn't easy by queries.
In this case however we could use a GroupBy query to get the needed results.
The needed "algoritm" is the fact that you're looking for ID's having two hits when selecting "A" and "B" and when selecting all records.
The easy way would be to make separate queries:
1) Select ID, Count(ID) as Counter from tblX where IDValue IN ("A","B") GroupBy ID Having Count(ID) = 2;
this will give the set of ID's with atleast "A" and "B", but we need to exclude those also having a "C"
2) Select ID, Count(ID) as Counter from tblX GroupBy ID Having Count(ID) = 2;
This query however will also return "A" and "C" as "valid".
3) JOIN the two above queries by ID to get the needed set of ID's and use that to display all data when needed.

Getting the idea ?

Nic;o)
Oct 10 '07 #2
GTXY20
29
Thanks so much - I get it - this seems to work.

When my queries are finalized I will post.
Oct 10 '07 #3
nico5038
3,080 Expert 2GB
Keep me posted !

Nic;o)
Oct 10 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: kevin parks | last post by:
hi. I've been banging my head against this one a while and have asked around, and i am throwing this one out there in the hopes that some one can shed some light on what has turned out to be a...
26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
10
by: Randell D. | last post by:
Folks, I have a SELECT that returns with multiple records - It works when I have a LIMIT clause but when I try to include a GROUP BY clause, the select returns nothing (ie no records, no...
11
by: Jeff Sandler | last post by:
I need a MySQL select statement as part of a PHP script. I want to find rows where a certain column either starts with or equals a user-supplied string. The string will be 1 or more characters in...
3
by: Sven Reifegerste | last post by:
Hi, i have a table with INT columns id,key,b1,b2,c1,c2, having 1.500.000 rows. 'key' and 'id' are indexed (Kardinality 385381) and id (Kardinality 1541525). Performing a SELECT * FROM...
8
by: Rich | last post by:
My table looks like this: char(150) HTTP_REF, char(250) HTTP_USER, char(150) REMOTE_ADDR, char(150) REMOTE_HOST, char(150) URL, smalldatetime TIME_STAMP There are no indexes on this table...
5
by: Marie | last post by:
Access97 I have a table containing addresses with a separate field for State. Is there a way to create a query that returns an unique list of the states in that table and still be updateable? I...
2
by: sammy | last post by:
If I want to return only unique values from an attribute in a table, how would I do so? The attribute may repeat across rows.
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
4
by: Mystagogue | last post by:
Given XML like this... <stuff> <thing id="1" result="true"/> <thing id="2" result="true"/> <thing id="3" result="false"/> <thing id="4" result="false"/> <thing id="5" result="error"/>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.