2012-04-12 53 views
1

我有一個缺席者的表,並且該表存儲那些缺席者的studentids。與內部連接的SQL計數

從這個表格中我必須找到總的參加者和總缺席者,爲此我剛加入了包含特定部分的最大容量的部分表格。

因爲我這個查詢是

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 

它的做工精細,同樣的道理我怎麼能找到性別明智presentees /缺席......性別列是學生表,任何人都可以給我一些想法,在此先感謝

回答

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 
+0

現在工作正常,非常感謝tobyodavies – shanish 2012-04-12 07:06:29