从间隔表中提取秒数/将记录转换为间隔? [英] Extract seconds from interval table / Cast record to interval?

查看:122
本文介绍了从间隔表中提取秒数/将记录转换为间隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过以下方式获取间隔时间:

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
 ...

这是我的方法:

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

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

所以我必须将记录(表"dt")强制转换为间隔?我怎么做?还是这是完全错误的?对不起,我是数据库查询的新手....

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....

推荐答案

要么

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

或者这个

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

这篇关于从间隔表中提取秒数/将记录转换为间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆