SQL Server使用PARTITION BY分區示例

--修改科目名到學生特長字段,分數大於140分,多個科目大於140分則取其中一個
SELECT ROW_NUMBER() OVER(PARTITION BY t.StudentID,t.StudentName ORDER BY t.CourseName) as GroupNum,
t.StudentID,t.StudentName,t.CourseName into #StudentGood from
(SELECT DISTINCT b.StudentID,b.StudentName,c.CourseName FROM StudentScore as a left join Student as b on a.StudentID=b.StudentID
 left join Course as c on a.CourseID=c.CourseID WHERE a.Score>140) as t;
 update Student set Good=b.CourseName from #StudentGood as b where Student.StudentID=b.StudentID and b.GroupNum=1
 drop table #StudentGood

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章