一千萬個為什麽

搜索

將時間戳打印為數字,而不是人類可讀


我需要一個針對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時間戳,以秒(或毫秒)為單位的兩邊整數。我已閱讀文檔,但無法弄清楚這一點。

最佳答案

我讀過你可以用它來做

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中查看日期和時間功能和操作

轉載註明原文: 將時間戳打印為數字,而不是人類可讀

猜你喜歡