如何解决获取别名的总和
试图计算别名 studentnotreviewed 的总和,解决这个问题的最佳方法是什么,我一直在尝试不同的东西。我想获取未审核的学生别名的总数。
select x.* from
(
select s.id as School,e.id as examID,e.exam_start as examstart,e.exam_end as examend,(select count(*) from students st where e.id=st.exam_id and (st.review_flag='' or st.review_flag is null)) as studentsnotreviewed,e.exam_end + Interval 2 day as 'DueDate',(select SUM(studentsnotreviewed)) as SUM
from exams e
#left join exams on st.exam_id = e.id
left join schools s on e.school_id=s.id
where e.exam_start < now() and e.exam_start>'2021-01-01' and e.practice=0) as x
where studentsnotreviewed>0 and (studentsnotreviewed>15 or examend < now())
order by duedate asc,studentsnotreviewed desc
[样本数据]
学校 | 考试ID | 考试开始 | 考试 | studentsnotreviewed | 截止日期 | 总和 |
---|---|---|---|---|---|---|
343 | 458092 | 2021 年 3 月 16 日下午 3:52 | 2017 年 8 月 28 日上午 12:59 | 2 | 2017 年 8 月 30 日,上午 12:59 | 2 |
125 | 360216 | 2021 年 1 月 7 日上午 11:55 | 2018 年 12 月 13 日晚上 11:17 | 27 | 2018 年 7 月 28 日晚上 11:17 | 27 |
[想要的结果] 未审核学生的别名总和。
未审核的学生总数 |
---|
29 |
解决方法
我相信元数据库支持 WITH,因此您可以将整个查询别名为临时数据块,然后将其作为表重用:
with x as
(
select
s.id as School,e.id as examID,e.exam_start as examstart,e.exam_end as examend,(select count(*) from students st where e.id=st.exam_id and (st.review_flag='' or st.review_flag is null)) as studentsnotreviewed,e.exam_end + Interval 2 day as 'DueDate',from
exams e
left join exams on st.exam_id = e.id
left join schools s on e.school_id=s.id
where e.exam_start < now() and e.exam_start>'2021-01-01' and e.practice=0
)
select
x.*,(select SUM(studentsnotreviewed) from x) as SUM
from
x
where
studentsnotreviewed>0 and
(studentsnotreviewed>15 or examend < now())
order by
duedate asc,studentsnotreviewed desc
您的查询似乎有一个轻微的语法错误 - 您在外部查询中提到了 #
而没有提到 students st
..我相信你可以解决这个问题
我假设您希望 29 作为在结果集中重复的值。如果你真的只想要一个 29 的值,我想它可以通过以下方式给出:
select count(*)
from
exams e
inner join students st on e.id=st.exam_id
where
(st.review_flag='' or st.review_flag is null) and
e.exam_start < now() and e.exam_start>'2021-01-01' and
e.practice=0
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。