将时间戳打印为数字,而不是人类可读的

时间:2010-05-07 10:40:57

标签: sql postgresql

我需要一个针对psql运行的命令行并执行:

psql database -c "select date_trunc('day', start) as date, avg(finish - start) as duration from things group by date order by date;"

但问题是这给了我人类可读的输出,又名:

    date         |    duration
---------------------+-----------------
 2010-01-11 00:00:00 | 00:24:13.826822
 2010-01-12 00:00:00 | 00:15:42.984878
 2010-01-13 00:00:00 | 00:15:37.117834
 2010-01-14 00:00:00 | 00:13:27.868552
 2010-01-15 00:00:00 | 00:09:16.018057
 2010-01-16 00:00:00 | 00:06:39.213929

我想要的是unix时间戳,以秒(或毫秒)为单位,两边都是整数。我已阅读文档,但无法弄明白。

1 个答案:

答案 0 :(得分:1)

我读到你可以用

来做
SELECT date_part('epoch', now()) 

给出unix时间戳

所以在你的情况下

SELECT date_part('epoch', date_trunc('day', start) as date), 
       date_part('epoch', avg(finish - start)) as duration 
FROM things 
GROUP by date 
ORDER by date;

请参阅postgres中的Date and time functions and operations