你好,我想显示两个日期之间的 SQL 差异:
* 结束日期 = dt_termino/now()
* 开始日期 = dt_inicio
我有一个函数可以用 CSV 编写...但在我的 csv 输出中,如:
id_task_tarefa | dt_inicio | dt_termino | sum
211 | 2016-01-25 10:40:25 | 2016-01-27 08:51:02 | 01 22:10:37
210 | 2016-01-25 10:40:29 | 2016-01-27 08:50:21 | 01 22:09:52
我需要一种在 Excel 中输出的格式 [h]:mm:ss;@ 像这样
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
这是我的 SQL:
SELECT id_task_tarefa,
dt_inicio,
dt_termino,
to_char(COALESCE(dt_termino::timestamp, now()::timestamp)
- dt_inicio::timestamp,'DD HH24:MI:SS')
FROM crm.task_interacao
最佳答案
这个功能可能对你来说很方便:
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'))
$$;
使用:
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)
关于postgresql - Excel 格式的两个日期 postgresql 之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38080314/