2013-11-10 27 views
0

搜索了很多之後,我要在這裏請教高手...指望條件在同一列

表1(考勤)

 
att_id att_date class_id student_id att_status 
    1  07-11-2013  1   1   P 
    2  07-11-2013  1   2   L 
    3  07-11-2013  1   3   P 
    4  07-11-2013  1   4   P 
    5  08-11-2013  2   5   P 
    6  08-11-2013  2   6   P 
    7  08-11-2013  2   7   A 
    8  08-11-2013  2   8   P 
    9  09-11-2013  1   1   L 
10  09-11-2013  1   2   P 
11  09-11-2013  1   3   A 
12  09-11-2013  1   4   P 
13  09-11-2013  2   5   P 

凡att_status P =目前,A =缺席, L =離開。現在我想統計個別學生出席,缺席和離開,根據班級編號

所需的結果

 
stu_id  class_id Present Absent Leave 
    1   1   1   0  1 
    2   1   1   0  1 
    3   1   1   1  0 
    4   1   2   0  0 
    5   2   1   0  0 
    6   2   1   0  0 
    7   2   0   1  0 
    8   2   1   0  0 

回答

3
select student_id, 
     class_id, 
     sum(att_status='P') as present, 
     sum(att_status='A') as absent, 
     sum(att_status='L') as leave 
from attendance 
group by student_id, class_id