If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Find the list of X for which there is only one Y?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-22-08, 18:53
Shaitan00 Shaitan00 is offline
Registered User
 
Join Date: Jan 2006
Posts: 13
Question Find the list of X for which there is only one Y?

I am trying to find an SQL query that will return a list of items in column X that only appear once, or more precisly only have 1 corresponding Y (X is key). For example:

Table (R):
X | Y
-----
0 a
1 a
1 b
2 c
2 d
2 e

In this case, I would expect to only get back "0" because it only has one corresponding Y value (a), whereas "1" has two (a & b) and "2" has 3 (c & d & e) ... etc....

Any help would be greatly appreciated...
Thanks,
Reply With Quote
  #2 (permalink)  
Old 06-22-08, 20:31
topcat2 topcat2 is offline
Registered User
 
Join Date: Jun 2008
Posts: 7
Assuming your table is called R and your columns are x and y:

Code:
select x from 
(select x, count(distinct y) nums from R group by x) y_count 
where nums = 1;
Reply With Quote
  #3 (permalink)  
Old 06-22-08, 20:39
Shaitan00 Shaitan00 is offline
Registered User
 
Join Date: Jan 2006
Posts: 13
Question

topcat2: thanks for the information. Quick question, do you know how to convert this to Relational Algebra?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On