bathurst_guy
05-07-2008, 12:20 AM
Hi,
I'm just working on an Assignment for Uni. Is it possible to GROUP BY something that isn't declared in the SELECT statement.
For example:
My Assignment Question is this:
List the names of all students who attended an event that was attended by a representative of the company "Perception".
My SQL so far:
SELECT `STUDENT`.`stu_fname` , `STUDENT`.`stu_lname` , `STUDENT`.`stu_no`
FROM `STUDENT` , `REP_ATTENDANCE` , `COMPANY` , `REPRESENTATIVE` , `STUDENT_ATTENDANCE`
WHERE `COMPANY`.`co_name` = 'Perception'
AND `REPRESENTATIVE`.`co_id` = `COMPANY`.`co_id`
AND `REP_ATTENDANCE`.`rep_id` = `REPRESENTATIVE`.`rep_id`
AND `STUDENT`.`stu_no` = `STUDENT_ATTENDANCE`.`stu_no`
GROUP BY `STUDENT`.`stu_no` Which returns the correct information: Carolyn Lodge 00002563
Joseph Witney 00005546
Jessica Wood 00008512
Daisy Blackmore 00250050
Sue Wilson 00501158
Iva Sawpit 02515400
Fred Frog 02551024
Robert Moffatt 05150025
Jenny Hammond 11051869
Ronald Smith 21554621
Ima Idjit 23515421
Danielle Blackmore 51892154
Errol Curry 55564444
Marie Chopping 87898887
Only problem is it also then returns the stu_no next to each students names, can I have it GROUP BY stu_no but not display it as an output? If I remove `STUDENT`.`stu_no` from the SELECT statement then it doesn't work either. Maybe there is a completely different way I should be doing this, with JOIN maybe? I don't understand JOIN yet though...
I'm just working on an Assignment for Uni. Is it possible to GROUP BY something that isn't declared in the SELECT statement.
For example:
My Assignment Question is this:
List the names of all students who attended an event that was attended by a representative of the company "Perception".
My SQL so far:
SELECT `STUDENT`.`stu_fname` , `STUDENT`.`stu_lname` , `STUDENT`.`stu_no`
FROM `STUDENT` , `REP_ATTENDANCE` , `COMPANY` , `REPRESENTATIVE` , `STUDENT_ATTENDANCE`
WHERE `COMPANY`.`co_name` = 'Perception'
AND `REPRESENTATIVE`.`co_id` = `COMPANY`.`co_id`
AND `REP_ATTENDANCE`.`rep_id` = `REPRESENTATIVE`.`rep_id`
AND `STUDENT`.`stu_no` = `STUDENT_ATTENDANCE`.`stu_no`
GROUP BY `STUDENT`.`stu_no` Which returns the correct information: Carolyn Lodge 00002563
Joseph Witney 00005546
Jessica Wood 00008512
Daisy Blackmore 00250050
Sue Wilson 00501158
Iva Sawpit 02515400
Fred Frog 02551024
Robert Moffatt 05150025
Jenny Hammond 11051869
Ronald Smith 21554621
Ima Idjit 23515421
Danielle Blackmore 51892154
Errol Curry 55564444
Marie Chopping 87898887
Only problem is it also then returns the stu_no next to each students names, can I have it GROUP BY stu_no but not display it as an output? If I remove `STUDENT`.`stu_no` from the SELECT statement then it doesn't work either. Maybe there is a completely different way I should be doing this, with JOIN maybe? I don't understand JOIN yet though...