SQL查询中的求和函数

拉姆鲁米·阿菲夫(Lamloumi Afif)

我有这张桌子Student

  1. ID(int,主键标识)
  2. 名称(varchar(20))
  3. 分数(整数)
  4. RecordDate(DateTime)

我需要选择该表的所有列以及一个额外的列,该列代表“得分”列的“总和”,具体取决于Name学生的情况。

我尝试了这个但是没用

select S.Id,S.Name ,S.Score ,S.RecordDate, ( select Sum(Score) from Student where Name= S.Name) as All_Student_Score
 from Student S;

如何更改此查询?

拉玛克
You can use a `JOIN`:

    SELECT S.*,
           T.All_Student_Score
    FROM Student S
    INNER JOIN (SELECT Name, SUM(Score) All_Student_Score
                FROM Student) T
        ON S.Name = T.Name;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章