|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Basic Join Problem
Hello,
I am not very experienced with SQL. I apologize if this is trivial. I have two tables: Class ----- ID (int) Teacher (nvarchar) Subject (nvarchar) Student -------- ID (int) ClassID (int) Name (nvarchar) I am trying to write a SQL query that will give me the total number of students in each class. I have tried the following: SELECT c.ID, c.Teacher, c.Subject COUNT(s.ID) FROM Class c LEFT OUTER JOINT ON Student s c.ID=s.ClassID Unfortunately, that gives me an error of: Column 'Class.Teacher' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. I'm afraid if I place everything in a GROUP BY clause, I will get some invalid results. Can anyone please steer me in the correct direction? Thank you! |
|
#2
|
|||
|
|||
|
Put all selected columns, except s.id, in the group by clause.
|
|
#3
|
|||
|
|||
|
Quote:
You can give this a try Code:
SELECT C.ID, C.TEACHER, C.SUBJECT, COUNT(S.ID) FROM CLASS C, STUDENT S WHERE C.ID = S.CLASSID GROUP BY C.ID, C.SUBJECT, C.TEACHER |
|
#4
|
||||
|
||||
|
Quote:
__________________
My blog about OpenSource Databases PDF tutorials about OSS databases, DBMonster ... Please contribute to Open Source Development, fill bug reports!!! Developer Shed eSupport Commented my.ini/my.cnf (PLEASE ADD YOUR OWN CONFIG TRICK) An introduction to database normalization Natural or Surrogate key Custom ordering for your results Correlated and uncorrelated subqueries Don't turn your outer joins into inner joins |
|
#5
|
|||
|
|||
|
Try this
Try This
Select count(a.id),b.classid from class c,student b where c.id=b.id and group by b.id,b.classid |
![]() |
| Viewing: Dev Shed Forums > Databases > Oracle Development > Basic Join Problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|