Created
March 30, 2017 15:24
-
-
Save gmile/6b85cede1051a532c0ec52b64b1cd92d to your computer and use it in GitHub Desktop.
Code for stackoverflow's problem
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
DROP TABLE IF EXISTS logs; | |
CREATE TABLE logs ( | |
id BIGSERIAL PRIMARY KEY, | |
status VARCHAR NOT NULL, | |
inserted_at DATE NOT NULL | |
); | |
INSERT INTO logs (status, inserted_at) VALUES | |
('created', '2017-01-01'), | |
('created', '2017-01-01'), | |
('closed', '2017-01-02'), | |
('created', '2017-01-02'), | |
('created', '2017-01-02'), | |
('created', '2017-01-03'), | |
('closed', '2017-01-03'), | |
('created', '2017-01-04'), | |
('created', '2017-01-05'), | |
('created', '2017-01-06'), | |
('created', '2017-01-07'), | |
('closed', '2017-01-08'); | |
SELECT days.day, | |
count(case when logs.inserted_at = days.day AND logs.status = 'created' then 1 end) as created, | |
count(case when logs.inserted_at = days.day AND logs.status = 'closed' then 1 end) as closed, | |
count(case when logs.inserted_at <= days.day AND logs.status = 'created' then 1 end) - | |
count(case when logs.inserted_at <= days.day AND logs.status = 'closed' then 1 end) as total | |
FROM (SELECT day::date FROM generate_series('2017-01-01'::date, '2017-01-10'::date, '1 day'::interval) day) days, | |
logs | |
GROUP BY days.day | |
ORDER BY days.day; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment