How to total the days according to a period?

0

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             |
+--------+--------------+--------------+----------------+
    
asked by anonymous 24.09.2014 / 17:16

1 answer

2

I think in your case it would be something like:

select codfun, min(data_ini), max(data_fim), sum(qtd_dias) from TABELA group by codfun;

I'm not sure how this will behave in mysql, but I do not think it has any difference with other banks.

    
24.09.2014 / 19:14