Last active
January 20, 2016 13:14
-
-
Save shreyas-satish/d721a3dbaafb4398163e to your computer and use it in GitHub Desktop.
Subquery using WITH
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
WITH cohort as ( | |
select author_id from articles where datetime >= '2015-01-01' and datetime <= '2015-01-31' | |
), return_rate as ( | |
select date_trunc('month', datetime) AS month, count(*) FROM articles WHERE author_id IN (SELECT author_id FROM cohort) AND datetime >= '2015-02-01' GROUP BY month ORDER BY count ASC | |
) | |
SELECT to_char(month, 'Mon YYYY'), count from return_rate; |
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 articles ( | |
id integer CONSTRAINT firstkey PRIMARY KEY, | |
author_id integer NOT NULL, | |
datetime timestamp without time zone | |
); | |
INSERT INTO articles VALUES (1, 1, '2015-01-01'); | |
INSERT INTO articles VALUES (2, 1, '2015-02-01'); | |
INSERT INTO articles VALUES (3, 1, '2015-03-01'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment