PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 1st, 2012, 03:08 PM
argo4242 argo4242 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 6 argo4242 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 37 sec
Reputation Power: 0
General SQL question

I have a pretty basic problem, but not too sql savy. I will keep it simple with the example vs using my real tables.

Imagine a table 'student' with columns 'id', 'name'.
Also a table 'classes' with columns 'id', 'student_fk', 'class_type'

student_fk is hopefully clearly the foreign key to the student.id primary key. I know this is not great design, but for the sake of this example it will do.

What i am looking for is a query to select only those students that have one class. Also that one class needs to be of a certain class_type.

The idea is that the student may have many classes of different types and i only want to find student that have ONLY 1 class associated with them of that type. If the student has a class of that type, but also has another class of a different type i don't want that student returned.

Hopefully this description makes sense. Any help is appreciated. Thanks in advance.

Reply With Quote
  #2  
Old August 1st, 2012, 04:41 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2012
Location: Germany
Posts: 2,047 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 h 5 m 45 sec
Reputation Power: 812
Hi,

Join both tables, group by the student ID and then select those students with exactly one row and the right class type.

Code:
SELECT
	student.id,
	student.name
FROM
	student
	INNER JOIN classes ON (student.id = classes.student_fk)
GROUP BY student.id
HAVING COUNT(*) = 1 AND EVERY(classes.class_type = ...);


Instead of "EVERY", you could also use "BOOL_OR". I don't know which one is faster in this case.

Reply With Quote
  #3  
Old August 2nd, 2012, 07:52 AM
argo4242 argo4242 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 6 argo4242 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 37 sec
Reputation Power: 0
Thanks!

Quote:
Originally Posted by Jacques1
Hi,

Join both tables, group by the student ID and then select those students with exactly one row and the right class type.

Code:
SELECT
	student.id,
	student.name
FROM
	student
	INNER JOIN classes ON (student.id = classes.student_fk)
GROUP BY student.id
HAVING COUNT(*) = 1 AND EVERY(classes.class_type = ...);


Instead of "EVERY", you could also use "BOOL_OR". I don't know which one is faster in this case.



Thanks for the speedy reply! If this works as expected I will respond back here to confirm.

Reply With Quote
  #4  
Old August 23rd, 2012, 01:03 PM
argo4242 argo4242 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 6 argo4242 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 37 sec
Reputation Power: 0
worked great!

Quote:
Originally Posted by argo4242
Thanks for the speedy reply! If this works as expected I will respond back here to confirm.


It worked like a champ. thanks.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > General SQL question

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap