PostgreSQL
 sql >> Base de Dados >  >> RDS >> PostgreSQL

Diferença entre duas datas postgresql no formato Excel


A função pode ser conveniente para você:
create or replace function interval_in_hours(interval)
returns text language sql as $$
    select format('%s:%s',
        (extract (epoch from $1) / 3600)::int,
        to_char($1, 'mi:ss'))
$$;

Usar:
with the_data (id_task_tarefa, dt_inicio, dt_termino) as (
    values
        (211, timestamp '2016-01-25 10:40:25', timestamp '2016-01-27 08:51:02'),
        (210, timestamp '2016-01-25 10:40:29', timestamp '2016-01-27 08:50:21') 
    )

select *, interval_in_hours(dt_termino- dt_inicio)::interval as sum
from the_data;

 id_task_tarefa |      dt_inicio      |     dt_termino      |   sum    
----------------+---------------------+---------------------+----------
            211 | 2016-01-25 10:40:25 | 2016-01-27 08:51:02 | 46:10:37
            210 | 2016-01-25 10:40:29 | 2016-01-27 08:50:21 | 46:09:52
(2 rows)