Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I am planning to execute a query with 4 table student, Markdetails,subject and department. I used the query

SQL
SELECT S.regno,S.studentname,S.gender,CM.classname,SM.subjectname,MS.mark FROM STUDENT S, MARKDETAILS MS,CLASSMASTER CM, SUBJECTNAME SM WHERE S.classid=CM.classid AND MS.subjectid=SM.subjectid AND  S.studentid=MS.studentid


And i got the output as

regno studentname gender classname subjectname mark
1 djsdsj 1 CSE TAMIL 50
1 djsdsj 1 CSE ENGLISH 51
1 djsdsj 1 CSE MATHS 53
1 djsdsj 1 CSE SCIENCE 54
1 djsdsj 1 CSE SOCIALSCIENCE 65
2 sdf 2 ECE TAMIL 61
2 sdf 2 ECE ENGLISH 62
2 sdf 2 ECE MATHS 63
2 sdf 2 ECE SCIENCE 64
2 sdf 2 ECE SOCIALSCIENCE 65

:confused: But my expected output is



regno studentname gender classname subjectname mark
1 djsdsj 1 CSE TAMIL 50
ENGLISH 51
MATHS 53
SCIENCE 54
SOCIALSCIENCE 65
2 sdf 2 ECE TAMIL 61
ENGLISH 62
MATHS 63
SCIENCE 64
SOCIALSCIENCE 65


Kindly help me in this.

Thanks in Advance.

Regards,
S.J.Parrthipan
Posted

The select command states SELECT S.regno,S.studentname,S.gender,CM.classname,SM.subjectname,MS.mark so these fields are returned for each row.

Read up on relational algebra and you'll understand why this is.

http://en.wikipedia.org/wiki/Relational_[^][^]

Good luck!
 
Share this answer
 
v2
Comments
Henry Minute 27-Sep-10 14:51pm    
You are of course correct but if the OP is unable to read the SQL Server documentation, I doubt this will actually help him. :)
AspDotNetDev 27-Sep-10 16:07pm    
I think you meant to link here: http://en.wikipedia.org/wiki/Relational_algebra
E.F. Nijboer 27-Sep-10 16:22pm    
Correct. I usually check pasted links because for some reason because this has a bug unfortunately. Must have forgotten it this time.
I should try a join somewhere like this :

SELECT top 100 S.regno, S.studentname, S.gender, CM.classname, SM.subjectname, MS.mark
FROM STUDENT S, SM.SUBJECTNAME

inner left join CLASSMASTER CM
on CM.classid=S.classid
inner left join MARKDETAILS MS
on MS.subjectid=SM.subjectid
AND S.studentid=MS.studentid

This probably don't work exactly but you can tweek it to get the desired effect.

Good luck
 
Share this answer
 
The output you are trying to accomplish is probably not supported by sql,

Because the rows returned from sql Query will have the same columns,
you will need some code side efforts to get a expected result.


Please vote and Accept Answer if it Helped.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900