Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 18th, 2006, 04:05 PM
gcoene@gmail.com
Guest
 
Posts: n/a
Default sql statement help

I have 2 tables:
1) employee - with a unique primary key - employeeid
2) training - with an autonumber primary key, second field employeeid,
third field training

data in employee table:
001
002
003
004
005
....

data in training table
1 001 CPR
2 001 WHMIS
3 002 CPR
4 004 CPR
5 005 WHMIS
....

Basically, I want to write an SQL statement that shows all of the
employees and whether or not they have taken CPR, for example:

001 CPR
002 CPR
003 null
004 CPR
005 null
....

Thanks for your help.

  #2  
Old December 18th, 2006, 07:05 PM
pietlinden@hotmail.com
Guest
 
Posts: n/a
Default Re: sql statement help


gcoene@gmail.com wrote:
Quote:
I have 2 tables:
1) employee - with a unique primary key - employeeid
2) training - with an autonumber primary key, second field employeeid,
third field training
>
data in employee table:
001
002
003
004
005
...
>
data in training table
1 001 CPR
2 001 WHMIS
3 002 CPR
4 004 CPR
5 005 WHMIS
...
>
Basically, I want to write an SQL statement that shows all of the
employees and whether or not they have taken CPR, for example:
>
001 CPR
002 CPR
003 null
004 CPR
005 null
...
>
Thanks for your help.
We did this in school, so I've done this absolutely to death. You're
missing a table. The standard setup is like this:
Student---(1,M)---Attendance---(M,1)---Course

and it looks like you only have the first two.

The solution is to create a sort of partial cartesian product between
Course and Student.

SELECT Course.*, Student.StudentID
FROM Course, Student
WHERE Course.CourseName='CPR';

will return a record for each (student,Course='CPR') combination. Now
subtract that result from the Students table, and you're gold.

SELECT Student.FirstName, Student.LastName
FROM Student
WHERE StudentID Not In (SELECT Student.StudentID
FROM Course, Student
WHERE Course.CourseName='CPR');

  #3  
Old December 18th, 2006, 07:15 PM
gcoene@gmail.com
Guest
 
Posts: n/a
Default Re: sql statement help

Beautiful! You're gold Jerry, gold!

 

Bookmarks

Thread Tools

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles