SunQuest
 
           General SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesGeneral SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old August 10th, 2004, 10:44 AM
EmmaS EmmaS is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 2 EmmaS User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
SQL programming question - should be simple!

It shoudl be very straightforwards - I hope i'm missing something obvious!

I can only describe this by example:

I have a table:
ID --------- item
100--------- A
100--------- B
100--------- C
101----------A
101----------B
101----------C
102----------A
102----------B
103----------A
103----------B
103----------C
103----------D

pretty boring table. Still, it has a default item list (A, B, C). I need to be able to interrogate the table for those IDs whose items differ from this default (whether by missing some items, or having too many, or having separate ones). I need to be able to interrogate it in many ways.

So far I have tried along the lines:

SELECT ID
FROM TABLE
HAVING ID= ALL
(SELECT ID FROM TABLE WHERE item IN ('A','B','C'))

and

SELECT DISTINCT ID
FROM TABLE
WHERE ID =
(SELECT ID
FROM TABLE
WHERE item IN ('A','B','C')
GROUP BY ID
HAVING COUNT(DISTINCT item) = 3)

Am I even remotely on the right lines? Or do I need to create a view for this? If so, any suggestions. More than a little stuck... ta

Reply With Quote
  #2  
Old August 10th, 2004, 12:29 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Maybe combine your second stab at it with a left join somehow to get the ones that *don't* match.
__________________
Please don't PM me asking for solutions outside the scope of a thread.
Keeping all responses in a thread stands to help others who come along later,
which is after all what this forum's all about.

Reply With Quote
  #3  
Old August 11th, 2004, 03:00 AM
EmmaS EmmaS is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 2 EmmaS User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
My feeling was that the second stab wouldn't work at all because it would select, for example

104-----A
104-----E
104-----F

I.e. it has ONE OF ('A','B','C'), and it also has a total of 3 items.

Is this not the case? Is it clever enough to apply the 'distinct count = 3' to the preceding specified items?
(when I run the SQL I get some odd results).
Thanks...

Reply With Quote
  #4  
Old August 11th, 2004, 04:06 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 956 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 651197 Folding Title: Super Ultimate Folder - Level 2Folding Points: 651197 Folding Title: Super Ultimate Folder - Level 2Folding Points: 651197 Folding Title: Super Ultimate Folder - Level 2Folding Points: 651197 Folding Title: Super Ultimate Folder - Level 2Folding Points: 651197 Folding Title: Super Ultimate Folder - Level 2Folding Points: 651197 Folding Title: Super Ultimate Folder - Level 2Folding Points: 651197 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 8 h 23 m 32 sec
Reputation Power: 4
Send a message via ICQ to Itsacon
I loved this question, as I just upgraded to MySQL 4.1, and this enabled me to test the subquery functionality they finally implemented.

This is the query you're looking for I think:

Code:
SELECT 	DISTINCT(table.id) 
FROM	table 
JOIN	(SELECT id, COUNT(id) AS cnt FROM table GROUP BY id) AS table2
USING 	(id)
WHERE	item NOT IN ('A', 'B', 'C')
OR	cnt <> 3;


This returns all the id's that have other than the normal set of A, B and C (so more, less or different characters).

Good luck.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > SQL programming question - should be simple!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway