2

I've been searching for quite a while and can't find an answer to this question:

I have a PostgreSQL table that is staged the following way:

Start Date | End Date   |  Name   | Team
-----------+------------+---------+------
2017-10-01 | 2017-10-10 | Person  |   1

And what I would like to is have each row a day between the start date and end date with the corresponding name and team of the person:

   Date     |  Name   |  Team
------------+---------+-------
2017-10-01  | Person  |   1
------------+---------+-------
2017-10-02  | Person  |   1
------------+---------+-------
2017-10-03  | Person  |   1

Is it even possible to do this with PostgreSQL? I'm currently running PostgreSQL 9.3.

1 Answer 1

5

You can use generate_series() for that:

select t.dt::date, p.name, p.team
from person p, generate_series(p.start_date, p.end_date, interval '1' day) as t(dt)
order by t.dt::date;

I don't have 9.3 around any more, but I think that should also work with that old version.

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.