I don’t even know where to start with this one, or if it’s even possible to do in PostgreSQL (or if I’ll just need to handle it in my code)
I have a table of Bills, which specify which day of the month each one is due
id | bill_name | day_of_month |
---|---|---|
1 | Mortgage | 5 |
2 | Car Payment | 11 |
3 | Water | 14 |
4 | Electricity | 27 |
and I have a table of 4 week Periods
id | start | end |
---|---|---|
1 | 07 Sep 2022 | 04 Oct 2022 |
2 | 05 Sep 2022 | 01 Nov 2022 |
3 | 02 Nov 2022 | 29 Dec 2022 |
I want to join the two tables together and determine if each bill falls within the start and end of a period and then assign its exact date
I want to end up with an exact_date column
start | end | bill_name | day_of_month | exact_date |
---|---|---|---|---|
07 Sep 2022 | 04 Oct 2022 | Car Payment | 11 | 11 Sep 2022 |
07 Sep 2022 | 04 Oct 2022 | Water | 14 | 14 Sept 2022 |
07 Sep 2022 | 04 Oct 2022 | Electricity | 27 | 27 Sep 2022 |
05 Oct 2022 | 01 Nov 2022 | Mortgage | 5 | 5 Oct 2022 |
05 Oct 2022 | 01 Nov 2022 | Car Payment | 11 | 11 Oct 2022 |
05 Oct 2022 | 01 Nov 2022 | Water | 14 | 14 Oct 2022 |
05 Oct 2022 | 01 Nov 2022 | Electricity | 27 | 27 Oct 2022 |
02 Nov 2022 | 29 Dec 2022 | Mortgage | 5 | 5 Nov 2022 |
02 Nov 2022 | 29 Dec 2022 | Car Payment | 11 | 11 Nov 2022 |
02 Nov 2022 | 29 Dec 2022 | Water | 14 | 14 Nov 2022 |
02 Nov 2022 | 29 Dec 2022 | Electricity | 27 | 27 Nov 2022 |
Is this possible? Any help in the right direction would be greatly appreciated
2
Answers
Use
generate_series()
:Fiddle here
You can cross join the tables and add the exact date(-1)
fiddle