I have the following table:
links :
created_at active 2017-08-12 15:46:01 false 2017-08-13 15:46:01 true 2017-08-14 15:46:01 true 2017-08-15 15:46:01 false
When a date range is given, I have to retrieve time series that tell me how many active links were created on a date equal to or less than the current (moving) date.
Output (for the date range 2017-08-12 - 2017-08-17):
day count 2017-08-12 0 (there are 0 active links created on 2017-08-12 and earlier) 2017-08-13 1 (there is 1 active link created on 2017-08-13 and earlier) 2017-08-14 2 (there are 2 active links created on 2017-08-14 and earlier) 2017-08-15 2 ... 2017-08-16 2 2017-08-17 2
I came up with the following query to create dates:
SELECT date_trunc('day', dd):: date FROM generate_series ( '2017-08-12'::timestamp , '2017-08-17'::timestamp , '1 day'::interval) dd
But rolling calculations confuse me and are not sure how to proceed. Can this be solved using a window function?
source share