开发者

Print out timestamp as number, not human readable

开发者 https://www.devze.com 2022-12-29 06:10 出处:网络
I need a command line that runs against psql and does: psql database -c \"select date_trunc(\'day\', start) as date, avg(finish - start) as duration from things group by date order by date;\"

I need a command line that runs against psql and does:

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

But the problem is this gives me human readable output, aka:

    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

What I want are unix timestamps in开发者_如何转开发 seconds (or milliseconds) as integers on both sides. I've read documentation but can't figure this out.


I read that you can do it with

SELECT date_part('epoch', now()) 

give unix timestamp

So in your case

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;

See Date and time functions and operations in postgres

0

精彩评论

暂无评论...
验证码 换一张
取 消