Extract seconds from interval table / Cast record to interval?

不羁岁月 提交于 2019-12-11 05:45:47

问题


Im getting interval times via:

SELECT time_col - lag(time_col) OVER (ORDER BY whatever) 
FROM myTable where conditions

This returns a table like this:

00:00:38
00:05:10
00:02:05
...

I want to have the time in seconds like this:

 38
 310
 125
 ...

Here is my approach:

 SELECT EXTRACT(epoch from dt) from (SELECT time_col - lag(time_col) OVER (ORDER BY whatever) FROM myTable where conditions) as dt

dt should be the table with the difference times (intervals). However I get the following error:

Error:  Function pg_catalog.date_part(unknown, record) does not exist

So I have to cast record (the table 'dt') to interval? How do I do that? Or is this completely wrong? Sorry Im new to database queries....


回答1:


Either this

SELECT EXTRACT(epoch from dt)
from (
    SELECT time_col - lag(time_col) OVER (ORDER BY whatever) dt
    FROM myTable
    where conditions
) as dt

Or this

SELECT
    extract(epoch from time_col - lag(time_col) OVER (ORDER BY whatever))
FROM myTable
where conditions


来源:https://stackoverflow.com/questions/16694615/extract-seconds-from-interval-table-cast-record-to-interval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!