带内连接的sql计数

时间:2012-04-12 06:53:01

标签: sql count inner-join

我有一张缺席者的表格,那张桌子正在存放那些缺席的人。

从这张表中我必须找到总的出席者和完全缺席者,为此我刚加入了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

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

1 个答案:

答案 0 :(得分: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