创建视图以获取在每个课程中获得最大分数的学生的姓名

时间:2012-06-07 05:12:03

标签: sql-server-2008 view

课程表:courseId,courseName,导师
学生表:studentId,studentName
标记表:marksId,studentId,courseId,marks
如果需要更多信息请注释

enter image description here


< \&BR GT;

CREATE VIEW maxmarks 
AS
SELECT b.studentName,courseId from [dbo].[zz_16_Marks_tbl] a,[dbo].[zz_16_Student_tbl] b
WHERE a.studentId=b.studentId AND marks in(
SELECT MAX(marks),courseId FROM [dbo].[zz_16_Marks_tbl] GROUP BY courseId)

1 个答案:

答案 0 :(得分:0)

CREATE VIEW maxmarks 
AS
WITH CTE AS (SELECT MAX(marks),courseId FROM [dbo].[zz_16_Marks_tbl] GROUP BY courseId )
SELECT b.studentName,a.courseId 
from [dbo].[zz_16_Marks_tbl] a
INNER JOIN [dbo].[zz_16_Student_tbl] b ON a.studentId=b.studentId 
INNER JOIN CTE c ON a.courseId =c.courseId and a.marks=b.marks