来自多个表的多个 COUNT SELECT 总数不起作用
Total of Multiple COUNT SELECTS from the multiple tables not working
我有一份出勤报告,我想在其中像这样显示报告日:
我可以单独计算Sum,但不能计算所有Sum的总和
我的学生出勤率Table:
ID ClassID SubjectID Day10 Day11 Day12 Day13 Day14 Day15 ProfessorID
215 23 46 P 36 36
216 23 47 P 36
217 23 48 P 36 P 36
218 17 35 P 28
我试过这个查询:
select ClassID,
sum(case when Day14= 'P' then 1 else 0 end) Present,
sum(case when Day14= 'A' then 1 else 0 end) Absent,
sum(case when Day14= 'L' then 1 else 0 end) Leave
from studentattendance
group by ClassID,Day14
我尝试了 Sum(Present.Absent,Leave)..但没有用 我缺少什么 ??
对于具有多个 FROM
子句(没有嵌套)的 SELECT
,没有 SQL 语法。
也许你想要这样的东西?
select
classID,
count(Day14) Total,
sum(case when Day14='P' then 1 else 0 end) Present,
sum(case when Day14='A' then 1 else 0 end) Absent,
sum(case when Day14='L' then 1 else 0 end) Leave
from studentattendance
group by classID
这里Total
只是每个classID
中的记录总数。 sum(case..when..)
语句仅在满足特定条件时模拟选择性 count()
(Day14
是 Present、Absent 或 Leave)。请注意,如果您希望 Day14
列中出现除这 3 个以外的值,则无法保证 Total
等于 Present+Absent+Leave
我有一份出勤报告,我想在其中像这样显示报告日:
我可以单独计算Sum,但不能计算所有Sum的总和
我的学生出勤率Table:
ID ClassID SubjectID Day10 Day11 Day12 Day13 Day14 Day15 ProfessorID
215 23 46 P 36 36
216 23 47 P 36
217 23 48 P 36 P 36
218 17 35 P 28
我试过这个查询:
select ClassID,
sum(case when Day14= 'P' then 1 else 0 end) Present,
sum(case when Day14= 'A' then 1 else 0 end) Absent,
sum(case when Day14= 'L' then 1 else 0 end) Leave
from studentattendance
group by ClassID,Day14
我尝试了 Sum(Present.Absent,Leave)..但没有用 我缺少什么 ??
对于具有多个 FROM
子句(没有嵌套)的 SELECT
,没有 SQL 语法。
也许你想要这样的东西?
select
classID,
count(Day14) Total,
sum(case when Day14='P' then 1 else 0 end) Present,
sum(case when Day14='A' then 1 else 0 end) Absent,
sum(case when Day14='L' then 1 else 0 end) Leave
from studentattendance
group by classID
这里Total
只是每个classID
中的记录总数。 sum(case..when..)
语句仅在满足特定条件时模拟选择性 count()
(Day14
是 Present、Absent 或 Leave)。请注意,如果您希望 Day14
Total
等于 Present+Absent+Leave