How to total the days according to a period?
Example:
+--------------+--------------+--------+----------+
| data_ini | data_fim | codfun | qtd_dias |
+--------------+--------------+--------+----------+
| '2014-01-15' | '2014-01-31' | 1 | 16 |
| '2014-02-01' | '2014-02-28' | 1 | 27 |
| '2014-03-01' | '2014-03-22' | 1 | 21 |
| '2014-03-10' | '2014-03-31' | 2 | 21 |
| '2014-04-01' | '2014-04-30' | 2 | 29 |
| '2014-05-01' | '2014-05-12' | 2 | 11 |
| '2014-04-08' | '2014-04-30' | 3 | 22 |
| '2014-05-01' | '2014-05-31' | 3 | 30 |
| '2014-05-01' | '2014-06-19' | 3 | 49 |
+--------------+--------------+--------+----------+
I need to total the column qtd_dias
by codfun
and bring the initial and final period (oldest date in data_ini
and latest date in data_fim
), for example:
+--------+--------------+--------------+----------------+
| codfun | data_ini | data_fim | qtd_dias_total |
+--------+--------------+--------------+----------------+
| 1 | '2014-01-15' | '2014-03-22' | 66 |
| 2 | '2014-03-10' | '2014-05-12' | 63 |
| 3 | '2014-04-08' | '2014-06-19' | 72 |
+--------+--------------+--------------+----------------+