连续时间戳对之间间隔的中值长度
Median length of interval between pairs of successive timestamps
我有一个 table 和 timestamp
列 (timestamp with time zone
)。我想知道每对连续时间戳之间间隔的中值长度。
例如,时间戳(同一天)为 08:00
、08:10
、08:30
、09:00
,我想要 20 minutes
,因为连续时间戳之间的中值间隔为 20 分钟。
我一直在尝试使用 window 函数和 lead/lag,但没有成功。
此代码可能有助于设置情况。
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
\.
尝试:
SELECT PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER by X) FROM
(
SELECT *,
timestamps - lag(timestamps) over (order by timestamps) As x
FROM Table1
) x
我有一个 table 和 timestamp
列 (timestamp with time zone
)。我想知道每对连续时间戳之间间隔的中值长度。
例如,时间戳(同一天)为 08:00
、08:10
、08:30
、09:00
,我想要 20 minutes
,因为连续时间戳之间的中值间隔为 20 分钟。
我一直在尝试使用 window 函数和 lead/lag,但没有成功。
此代码可能有助于设置情况。
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
\.
尝试:
SELECT PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER by X) FROM
(
SELECT *,
timestamps - lag(timestamps) over (order by timestamps) As x
FROM Table1
) x