1

I have a table "performances" with columns "date" and "count". However, the rows are sparse i.e. there are many days for which there is no row, which implicitly means that the count = 0. Is there a query I can do that when run on this:

date       count
2016-7-15  3
2016-7-12  1
2016-7-11  2

Would give me this:

date       count
2016-7-15  3
2016-7-14  0
2016-7-13  0
2016-7-12  1
2016-7-11  2

?

1

1 Answer 1

2

You can use generate_series() and a left join:

with q as (<your query here>)
select s.dte, coalesce(q.count, 0) as count
from (select generate_series(min(q.date), max(q.date), interval '1 day') as dte
      from q
     ) s left join
     q
     on s.dte = q.date;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.