I have two tables; student
and attendance
:
Student table:
sid name
----- --------
s1 nam1
s2 nam2
s3 nam3
s4 nam4
s5 nam5
Attendance table:
sid status date sub_id
----- -------- --------- ------
s1 present 2017-05-16 ms100
s2 present 2017-05-16 ms100
s3 absent 2017-05-16 ms100
s4 present 2017-05-16 ms100
s5 present 2017-05-16 ms100
s1 present 2017-05-17 ms100
s2 present 2017-05-17 ms100
s3 absent 2017-05-17 ms100
s4 present 2017-05-17 ms100
s5 absent 2017-05-17 ms100
s1 present 2017-05-16 ms101
s2 present 2017-05-16 ms101
s3 absent 2017-05-16 ms101
s4 present 2017-05-16 ms101
s5 absent 2017-05-16 ms101
Now I want to show on which date students were present or absent also want to count the total attended class, consecutive absent and percentage of attendance for every student for the subject id ms100.
For consecutive missing/absent i want to to consider only last consecutive missing/absent.For example if among 10 days s1 was present on day 1, 6, 7 then his con_missing will be 3
not 5
. If s1 was present on day 9 then his con_missing will be 0 as i want to consider consecutive missing only when a student is absent for more than 1 days.
For example students with sid s1,s2,s4
were present in all the 2 classes of the subject ms100
so their total number of attended class
will be 2, as the were present in all classes so their total number of consecutive absent
will be 0
and percentage
will be {(total attended class/total class)*100}
which is 100 %
in this case. On the other hand s3
was absent in all classes so his total number of attended class
and percentage
will be 0
and total number of consecutive absent
will be 2.
For student id s5 the consecutive absent
will be 0
as he is absent just for one day.
I am expecting result like following pattern where each individual class date for the subject ms100
will be shown as column and attendance status(present/absent) of an individual student on that particular date will be shown as value of that column:
sid name 2017-05-16 2017-05-17 consecutive_absnt total_atn %
----- ----- ---------- --------- ---------------- --------- ----
s1 nam1 present present 0 2 100
s2 nam2 present present 0 2 100
s3 nam3 absent absent 2 0 0
s4 nam4 present present 0 2 100
s5 nam5 present absent 0 1 50
I am using Angularjs
as font end and php as back end.This is what i have tried so far
php
$query="
SELECT atn.sid
, atn.date
, atn.status
, s.name
FROM attendance atn
join student s
on atn.sid = s.sid
where atn.sub_id = 'ms100'
ORDER
BY atn.date
, atn.sid
";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$arr = array();
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$arr[] = $row;
}
}
# JSON-encode the response
$json_response = json_encode($arr);
// # Return the response
echo $json_response;
angularjs
<table class="table table-striped table-bordered">
<thead>
<th>sid</th>
<th>name</th>
<th ng-repeat="data in list | unique: 'date'">{{data.date}}</th>
<th>consecutive missing</th>
<th>total attended </th>
<th>%</th>
</thead>
<tbody>
<tr ng-repeat="data in filtered = (list | filter:search | orderBy : predicate :reverse) | startFrom:(currentPage-1)*entryLimit | limitTo:entryLimit">
<td>{{data.sid}}</td>
<td>{{data.name}}</td>
<td>{{data.status}}</td>
<td>{{data.consecutive}}</td>
<td>{{data.total_atn}}</td>
<td>{{data.percentage}}</td>
</tbody>
</table>
Getting result like this
sid name 2017-05-16 2017-05-17 con_missing totl_atend %
----- -------- --------- ---------- -------- -------- ---
s1 nam1 present
s2 nam2 present
s3 nam3 absent
s4 nam4 present
s5 nam5 present
s1 nam1 present
s2 nam2 present
s3 nam3 absent
s4 nam4 present
s5 nam5 absent
So how can I achieve my expected result through mysql query?