【问题标题】:Median length of interval between pairs of successive timestamps连续时间戳对之间间隔的中值长度
【发布时间】:2018-01-10 05:27:46
【问题描述】:

我有一张带有 timestamp 列 (timestamp with time zone) 的表。我想知道每对连续时间戳之间的间隔的中值长度。

例如,时间戳(在同一天)为08:0008:1008:3009:00,我想要20 minutes,因为连续时间戳之间的中间间隔是 20 分钟。

我一直在尝试使用窗口函数和领先/滞后的东西,但没有成功。

此代码可能有助于设置情境。

CREATE TABLE timestamps (
    "timestamp" timestamp with time zone
);
COPY timestamps ("timestamp") FROM stdin;
2017-08-02 08:00:53.550685-00
2017-08-02 08:10:53.550685-00
2017-08-02 08:30:53.550685-00
2017-08-02 09:00:53.550685-00
\.

【问题讨论】:

    标签: sql postgresql datetime window-functions postgresql-9.5


    【解决方案1】:

    试试:

    SELECT PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER by X) FROM 
    (
    SELECT *,
           timestamps - lag(timestamps) over (order by timestamps) As x
    FROM Table1
    ) x
    

    演示:http://sqlfiddle.com/#!17/9bfe9/5

    【讨论】:

      猜你喜欢
      • 2015-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 2015-11-14
      • 2014-07-12
      相关资源
      最近更新 更多