i have a table named attendance with 2 attributes (id, remarks). i want to display the tally of absence or late per id from the attendance table.
Attendance Table
|ID | Remarks |
=============================
|1 | Absent |
|1 | Late |
|2 | Absent |
|2 | Absent |
|3 | Late |
Sample Output
|ID | Absent | Late |
==================================
|1 | 1 | 1 |
|2 | 2 | |
|3 | | 1 |
currently, i can only output 2 columns, (ID and Absent) or (ID and Late) using this code:
SELECT id, count(remarks) AS Absent
FROM attendance
WHERE remarks = 'Absent'
GROUP BY id;
i can’t display absent and late column simultaneously.. please help. thanks.
This is basically a
PIVOT. If you do not have access to aPIVOTfunction then, you can replicate it with an aggregate function and aCASEstatement:See SQL Fiddle with Demo
Or you can use
COUNT():See SQL Fiddle with Demo