統計某個時間範圍內的前幾行
在PostgreSQL 中,您可以確定每個時間範圍內給定時間範圍內先前記錄的總數使用視窗函數進行行。
在視窗函數中使用RANGE(Postgres 11 或較新版本)
對於Postgres 11 或更高版本,RANGE 模式可讓您使用PRECEDING 和EXCLUDE 選項指定時間範圍:
SELECT id, ts , count(*) OVER (ORDER BY ts RANGE '1 hour' PRECEDING EXCLUDE CURRENT ROW) FROM test ORDER BY ts;
舊版PostgreSQL 版本
對於早期版本的PostgreSQL,其他方法是建議:
羅馬查詢(ROM)
SELECT id, ts , (SELECT count(*)::int - 1 FROM unnest(dates) x WHERE x >= sub.ts - interval '1h') AS ct FROM ( SELECT id, ts , array_agg(ts) OVER (ORDER BY ts) AS dates FROM test ) sub;
數組計數(ARR)
SELECT id, ts , (SELECT count(*) FROM test t1 WHERE t1.ts >= t.ts - interval '1h' AND t1.ts < t.ts) AS ct FROM test t ORDER BY ts;
數組計數(ARR)
CREATE OR REPLACE FUNCTION running_window_ct(_intv interval = '1 hour') RETURNS TABLE (id bigint, ts timestamp, ct int) AS $func$ DECLARE cur CURSOR FOR SELECT t.ts + _intv AS ts1 , row_number() OVER (ORDER BY t.ts ROWS UNBOUNDED PRECEDING) AS rn FROM test t ORDER BY t.ts; rec record; rn int; BEGIN OPEN cur; FETCH cur INTO rec; ct := -1; FOR id, ts, rn IN SELECT t.id, t.ts, row_number() OVER (ORDER BY t.ts ROWS UNBOUNDED PRECEDING) FROM test t ORDER BY t.ts LOOP IF rec.ts1 >= ts THEN ct := ct + 1; ELSE LOOP FETCH cur INTO rec; EXIT WHEN rec.ts1 >= ts; END LOOP; ct := rn - rec.rn; END IF; RETURN NEXT; END LOOP; END $func$;
SELECT * FROM running_window_ct('1 hour');
以上是如何在 PostgreSQL 中統計某個時間範圍內的前一行?的詳細內容。更多資訊請關注PHP中文網其他相關文章!