I have a table called "rentals" in which I store data like the following:
id | rent_id | start_date | end_date | amount
---------------------------------------------
1 | 54 | 12-10-2019 | 26-10-2019| 100
2 | 54 | 13-10-2019 | 20-10-2019| 150
What do I expect? A result like:
12-10-2019 , amount 100
from 13-10-2019 to 20-10-2019, amount 250
from 21-10-2019 to 26-10-2019, amount 100
Basically I want, for each day , the sum of amount. But I also want to calculate "days between".
So the expected result would be:
id | rent_id | day | amount
---------------------------------------------
1 | 54 | 12-10-2019 | 100
2 | 54 | 13-10-2019 | 250
3 | 54 | 14-10-2019 | 250
and so on...
I'm actually running the following sql:
select start_date, ( select sum(amount) from rentals as t2 where t2.start_date <= t1.start_date) as amount from rentals as t1 WHERE rent_id = 54 group by start_date
but the result is not like expected...
I'm using MySQL.