Skip to content

Instantly share code, notes, and snippets.

@pokarim
Forked from choplin/continuous_count.sql
Last active January 2, 2016 13:49
Show Gist options
  • Save pokarim/8312840 to your computer and use it in GitHub Desktop.
Save pokarim/8312840 to your computer and use it in GitHub Desktop.
CREATE TABLE todesking (created_at text, status int);
INSERT INTO todesking VALUES
('01-01', 0),
('01-02', 0),
('01-03', 1),
('01-04', 0)
;
SELECT
row_number - 1 as num
FROM (
SELECT
created_at,
status,
row_number() OVER (ORDER BY created_at ROWS UNBOUNDED PRECEDING)
FROM
todesking
) t
WHERE status != 0 FETCH FIRST 1 ROW ONLY
;
num
-----
2
(1 row)
SELECT
min(row_number) -1 as num
FROM (
SELECT
created_at,
status,
row_number() OVER (ORDER BY created_at ROWS UNBOUNDED PRECEDING)
FROM
todesking
) t
WHERE status != 0
;
num
-----
2
(1 row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment