带内连接的sql计数

sha*_*ish 1 sql inner-join count

我有一张缺席表,这张表存放了那些缺席的人.

从这张表中我必须找到完整的出席者和完全缺席者,为此我刚加入了Sections表,其中包含特定Section的最大容量.

为此我的查询是

select COUNT(Attendance.studentid) as Absentees
        ,Sections.Max-count(studentid) as Presentees
from Attendance
inner join Students
on students.StudentId=Attendance.StudentId
inner join Sections
on Sections.CourseId=students.CourseId
group by Sections.Max
Run Code Online (Sandbox Code Playgroud)

它的工作正常,同样如何找到性别明智的出席者/缺席者......性别专栏在学生表中,谁能给我一些想法,提前谢谢

tob*_*ies 5

只需将性别列添加到您的select ...列中group by,您最终会为每个性别添加一行:

select COUNT(Attendance.studentid) as Absentees,
       Sections.Max-count(studentid) as Presentees,
       Students.Gender as Gender
from Attendance
inner join Students
on Students.StudentId=Attendance.StudentId
inner join Sections
on Sections.CourseId=Students.CourseId
group by Sections.Max, Students.Gender
Run Code Online (Sandbox Code Playgroud)