我有一个名为effective
的表,它有id、开始时间、结束时间。
我想计算所有行的日期差异。我试过像下面的方法,
select DATE_PART('hour', age(endtime,starttime )) OVER (PARTITION BY id) as increase from effective
但是显示如下错误
ERROR: OVER specified, but date_part is not a window function nor an aggregate function
我想要这样的结果,
id || starttime || endtime || hour
1 || '2017-09-15 14:50:39.312' || '2017-09-15 16:50:34.349' || 2
2 || '2017-09-15 14:50:34.349' || '2017-09-15 15:55:48.894319' || 1
最佳答案
使用 date_part
并减去时间。
SELECT id, starttime, endtime, date_part('hour',endtime-starttime) AS hour
FROM effective
输出
id starttime endtime hour
1 2017-09-15T14:50:39Z 2017-09-15T16:50:34Z 1
2 2017-09-15T14:50:34Z 2017-09-15T15:55:48Z 1
SQL fiddle :http://sqlfiddle.com/#!15/916ac9/2/0
此外,ID 1 从头到尾只有 1 小时 59 分 55 秒,而不是 2 小时。
关于sql - 如何计算 PostgreSQL 表中所有行的日期差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46241974/