Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sureshsmanian
Starting Member
31 Posts |
Posted - 2011-06-28 : 13:49:55
|
| Hi everyone,I have two tablesStudent (RegNo - PK, Name)Subject (ID, StudentId(refers the Student table), SubjectName)Student-------RegNo Name ----- ----10 Amit20 Pankaj30 KumarSubject-------ID StudentId SubjectName---------------------------1 10 English2 10 Maths3 10 Physics4 20 Economics5 30 Physics6 30 BiologyI want the output in the following formatStudentId Name Count(Subjects)--------------------------------10 Amit 320 Pankaj 1 30 Kumar 2 Thanks for your help.RegardsSSM |
|
|
Dasman
Yak Posting Veteran
79 Posts |
Posted - 2011-06-28 : 13:54:50
|
| First an Inner Join at the Student ID = RegNo Then a GRoup By statement. Use this as an aid: http://www.w3schools.com/sql/sql_groupby.aspDasman==========================Pain is Weakness Leaving the Body. |
 |
|
|
Dasman
Yak Posting Veteran
79 Posts |
Posted - 2011-06-28 : 13:59:35
|
| This should work:Select RegNo, Name FROM StudentInner Join SubjectON Subject.StudentID = Student.RegNoGroup By Name==========================Pain is Weakness Leaving the Body. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-06-28 : 14:04:32
|
quote: Originally posted by Dasman This should work:Select RegNo as StudentId, Name,count(*) as [Count(Subjects)] FROM StudentInner Join SubjectON Subject.StudentID = Student.RegNoGroup By Name==========================Pain is Weakness Leaving the Body.
No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|