I would like to know how to realize query which selects sum of time column grouped by clientid column WHERE clause is divided by months and department from the same table:
table is the following:
The goal here is to have final result like the following:
result 1.0
------------------------------
clientid | december| february
------------------------------
1 | 2:20 | 4:00
------------------------------
2 | 4:00 | 7:00
------------------------------
Result will be displayed in the following manner:
<table>
<thead>
<tr>
<th>Clientes</th>
<th>January</th>
<th>February</th>
</tr>
</thead>
<tbody>
<?php foreach($stmt as $row)
{
echo"
<tr>
<td>".$row['clientid']."</td>
<td>".$row['january']."</td>
<td>".$row['february']."</td>
</tr>"; }; ?>
</tbody>
</table>
In the following case scenario of query:
SELECT t.clientid, t.department,
(SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `time` ) ) ) FROM time_management WHERE YEAR(date)='$year' AND MONTH(date)= 12 AND department = '$department' GROUP by clientid ) as january,
(SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `time` ) ) ) FROM time_management WHERE YEAR(date)='$year' AND MONTH(date)= 2 AND department = '$department' GROUP by clientid ) as february
FROM time_management t WHERE t.department = '$department' GROUP by t.clientid
I get error: SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row
Since the sub query's are returning more than one row.
Without GROUP BY inside the sub query's the result is the following:
1 06:20:00 11:00:00
2 06:20:00 11:00:00
So my question is how to structure the query in order to get the desired result showed in result 1.0
Any help will be very welcome. Thank you!