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 |
pinkysam
Starting Member
2 Posts |
Posted - 2014-09-23 : 15:19:12
|
Table :StudentTeacherRelationId StdId TeacherName Day subject1 1 Archana Monday English 2 1 Archana Tue Marathi3 1 Shama Wed Hindi4 1 shama Thus Hindi5 1 Kavita Fri Hindi6 2 Archana Mon english7 2 Dipti Tues HindiSecond table : StudentId Sname Cid1 Shalini 12 Monika 13 Rohan 3 I want to fetch uniq combination of stuid and subject.Result should show all subject of student whether may be teachername and day. If I choose shalini whose stuid is 1,all subject for shalini(hindi,english,marathi) should come. Record from either of three should come Id StdId TeacherName Day subject 3 1 Shama Wed Hindi 4 1 shama Thus Hindi 5 1 Kavita Fri HindiI want fetch studentname along with teachername,day and subject whose cid = 1 here is my queryselect Student.Sname,TeacherName, Day,subjectfrom StudentTeacherRelationinner join StudentStudent.id = StudentTeacherRelation.StuIdwhere cid = 1I want place result of it in temp,Want fetch max(id) from temp table by doing group by on Sname and Subject.find all id from temp table where that id present in max id.show Id StdId TeacherName Day subject where (1,2,3,4,5,6,7-- all id from temp) in (1,2,5,6,7 -- max id from temp by doing group by on Sname and subject)// So it will show record Id StdId TeacherName Day subject where id is 1,2,5,6,7.Only five record should come.How to do that? |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2014-09-23 : 15:35:10
|
I don't know why you want to use a temp table. Surely this will do it:select Student.Sname,TeacherName, Day,subject, max(stdId)from StudentTeacherRelationinner join StudentStudent.id = StudentTeacherRelation.StuIdwhere cid = 1group by Sname, Subject, TeacherName, Day Note that you need to add TeacherName, Day to the GROUP BY since they are not aggregated. OTOH you could aggregate them with MAX or MIN or whatever you want. |
|
|
|
|
|
|
|