i have 3 tables.
table 1:
student ---------- studentid studentname
table 2:
studentmarks ---------------- studentid subjectid marks
table 3:
subject --------- subjectid subjectname
i need query top 3 students in each subject.
i want output like:
---------------------------------------------------------------- studentid name subjectid subjectname marks ----------------------------------------------------------------- 1 aaa 1 phy 85 2 bbb 1 phy 75 3 ccc 1 phy 65 3 ccc 2 bio 85 4 ddd 2 bio 75 1 aaa 2 bio 65 6 fff 3 che 85 1 aaa 3 che 75 5 eee 3 che 65 3 ccc 4 mat 85 2 bbb 4 mat 75 4 ddd 4 mat 65
with cte ( select s.studentid, s.studentname, sm.subjectid, j.subjectname, sm.marks, row_number() over(partition sm.subjectid order sm.marks desc) rn student s inner join studentmarks sm on s.studentid = sm.studentid inner join subject j on sm.subjectid = j.subjectid ) select studentid, studentname, subjectid, subjectname, marks cte rn <= 1;
Comments
Post a Comment