Обновление на SQL сервере с использованием AVG и MAX - PullRequest
0 голосов

Кто-нибудь может мне помочь?

У меня две таблицы Results и Students показаны здесь:

StudentID   CourseID   Year     Semester    Mark 
------------------------------------------------
   S001       DS01     2017        1        3 
   S001       DS01     2017        2        6   
   S001       AI01     2017        1        4.5 
   S001       AI01     2017        2        6 
   S001       CN01     2017        3        5 
   S002       DS01     2016        1        4.5 
   S002       DS01     2017        1        7 
   S002       CN01     2016        3       10 
   S002       DSA1     2016        3        9 
StudentID   LastName    FirstName   Sex     DateOfBirth     PlaceOfBirth    DeptID  Scholarship     AverageScore
S001    Lê  Kim Lan     F   23/02/1990  Hà nội  IS  130000  
S002    Trần Minh Chánh     M   24/12/1992  Bình Định   NC  150000  

(AverageScore теперь равен нулю)

Я хочу обновить идентификатор курса - вы можете мне помочь?

Это мой код:

update Students
set AverageScore = (select avg(max(Mark).CourseID) 
                    from Results 
                    where Results.StudentID = Students.StudentID)

Ответы [ 2 ]

0 голосов
/ 11 июля 2020

вы можете попробовать это:

;WITH aa AS(
 SELECT resultid,CourseID,MAX(Mark) mark FROM result GROUP BY resultid,CourseID )
UPDATE dbo.student SET AverageScore=(select AVG(mark) from aa WHERE aa.resultID=student.resultID)
select * from dbo.student
0 голосов
/ 11 июля 2020

Вы можете использовать следующий подзапрос:

update Students set AverageScore = (select avg(marks) from (
     Select max(mark) as marks
     from Results 
     where Results.StudentID = Students.StudentID  group by Results.courseid  ) )
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...