sql - 在 sqlite 中选择最大值的总和
问题描述
基于下表架构,我希望能够选择每次提交的最大值之和。一个学生可以有多个参赛作品,因此他们的总分是每场比赛的最高分之和。现在我的查询选择了他们所有比赛的 student_id、姓名和最高分,我怎样才能得到他们所有比赛的最高分的总和?基于输入的预期输出应该是:
select student_id, name, max(score) as TotalScore
from students2 as st
join submissions as s
where st.student_id = s.student_id
group by st.student_id, name
having count(*) > 1
order by TotalScore desc;
解决方案
select student_id, name, sum(TotalScore) total_sum
from (select st.student_id student_id
, name
, max(score) as TotalScore
from students as st
join submissions as s
on st.student_id = s.student_id
group by s.contest_id, s.student_id)
group by student_id;
我已经开始在您的示例数据之前创建一个演示......
从您问题中的文字中,我不明白您需要什么having count(*) > 1
,所以我没有使用它。
请注意:我已经使用students
了作为表的名称而不是students2
。
推荐阅读
- tcl - TclOO:将变量声明为“类”级别或在构造函数中的区别
- node.js - 根据环境在 ReactJS 中加载属性
- c# - 如何录制和访问 Microsoft Teams 会议音频?
- authentication - Google colab 无法使用 PyDrive 对 Google Drive 进行身份验证
- javascript - 根据列排序 Google 表格
- ios - 一旦添加了多个 UIExfields 或约束并运行应用程序,视图控制器就会全白
- java - 如何在 AWS Elastic Beanstalk 中访问 Spring Boot 应用程序的 X-Forwarded-For 标头?
- c# - BotFramework - 错误 401 - 未经授权 - 在具有 AppId 和 Secret 的模拟器上工作,但在 Azure 中不起作用
- javascript - div 内容不随父移动
- django - 有条件地阻止 Django 加入链式序列化程序