-
-
Save pokarim/8312840 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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