【问题标题】:Postgres : find the minimum of difference between timestamp rowsPostgres:找到时间戳行之间的最小差异
【发布时间】:2015-09-08 16:15:05
【问题描述】:

我有用户,他们跑步,我有每圈的时间戳记录。

我想按最快圈速对用户进行排序。

想法是使用postgres的“滞后”功能,带有“GROUP BY user_id”

我尝试了一些东西,但我不太了解“滞后”,而且我无法获取和窗口函数 (lap) 与聚合函数 (min) 一起使用

http://sqlfiddle.com/#!15/ec9dc/6

你有什么想法吗?

谢谢

【问题讨论】:

标签: postgresql function timestamp


【解决方案1】:

如果您想按用户的单圈持续时间对用户进行排序,您必须按 user_id 列对行进行分区。否则不同用户的单圈时间会混在一起。

我采用了您的示例代码并对其进行了一些修改:我删除了一个时间戳,该时间戳被插入了两次,并且我引入了一个包含窗口函数rank()lag() 的视图。前者用于计算每个用户的圈数,后者用于确定当前时间戳的前一个时间戳。

BEGIN;

CREATE TABLE lap
(
  user_id text,
  timestamp timestamp without time zone
);

INSERT INTO lap VALUES
    ('a', '2015-08-20 16:14:30.568'),
    ('a', '2015-08-20 16:16:13.06'),
    ('b', '2015-08-20 16:16:18.06'),
    ('b', '2015-08-20 16:16:25.63'),
    ('b', '2015-08-20 16:17:10.568'),
    ('a', '2015-08-20 16:17:25.63'),
  --('a', '2015-08-20 16:17:34.087'), -- Timestamp was inserted twice.
    ('a', '2015-08-20 16:17:34.087');

CREATE OR REPLACE VIEW user_lap_duration AS
    SELECT
        user_id,
        -1+rank() OVER w AS lap_nr,
        lag(timestamp) OVER w AS timestamp_prev,
        timestamp,
        timestamp - lag(timestamp)
            OVER w
          AS lap_duration
    FROM
        lap
        WINDOW w AS (PARTITION BY user_id ORDER BY timestamp);

SELECT
    user_id,
    lap_nr,
    lap_duration
FROM
    user_lap_duration
WHERE
    lap_nr <> 0
ORDER BY
    lap_duration;

ROLLBACK;

运行上面的代码会产生以下输出。

 user_id | lap_nr | lap_duration 
---------+--------+--------------
 b       |      1 | 00:00:07.57
 a       |      3 | 00:00:08.457
 b       |      2 | 00:00:44.938
 a       |      2 | 00:01:12.57
 a       |      1 | 00:01:42.492
(5 rows)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多