PDA

View Full Version : [MySQL] Raggruppare ore di lavoro per mesi


bouncey2k
04-01-2015, 22:38
Salve,
ho necessità di calcolare il numero delle ore svolte da dei dipendenti nell'arco di un anno, e raggrupparle per mese.
Ogni riga deve essere così formata:

Dipendente 1 | totale ore gen | totale ore feb | etc... | totale anno 2014
Dipendente 2 | totale ore gen | totale ore feb | etc... | totale anno 2014
Dipendente 3 | totale ore gen | totale ore feb | etc... | totale anno 2014

Presupponendo che nel DB io ho un campo "tessera", "dataora_entrata", "dataora_uscita", sono riuscito solo a calcolare il totale in un anno, ma senza distinguere tra i mesi.


SELECT
tessera,
SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(dataora_uscita, dataora_entrata)))) as Timediff
FROM
presenze
WHERE
YEAR(dataora_entrata) = '2014'


Grazie a chi mi potrà aiutare.

bouncey2k
05-01-2015, 20:30
up?

oNaSsIs
06-01-2015, 17:56
Ci sono dei warnings, vedi se riesci ad eliminarli.

mysql> select * from employee;
+------+---------------------+---------------------+
| name | time_in | time_out |
+------+---------------------+---------------------+
| John | 2015-01-01 08:00:00 | 2015-01-01 17:00:00 |
| John | 2015-02-01 08:00:00 | 2015-02-01 17:00:00 |
| Tim | 2015-02-01 08:00:00 | 2015-02-01 17:00:00 |
| John | 2015-02-03 08:00:00 | 2015-02-03 17:00:00 |
+------+---------------------+---------------------+
4 rows in set (0.00 sec)

mysql> select name,
-> SEC_TO_TIME(sum(if(month(time_in)=1,TIME_TO_SEC(TIMEDIFF(time_out, time_in)),'00:00:00'))) as Gennaio,
-> SEC_TO_TIME(sum(if(month(time_in)=2,TIME_TO_SEC(TIMEDIFF(time_out, time_in)),'00:00:00'))) as Febbraio
-> from employee group by name;
+------+----------+----------+
| name | Gennaio | Febbraio |
+------+----------+----------+
| John | 09:00:00 | 18:00:00 |
| Tim | 00:00:00 | 09:00:00 |
+------+----------+----------+
2 rows in set, 4 warnings (0.00 sec)