I have this activity table
+--------------+------------------+
| Field | Type |
+--------------+------------------+
| id | int(11) unsigned |
| start_date | timestamp |
| end_date | timestamp |
| ... | |
+--------------+------------------+
I need a view which groups these activities by start_date by DAY, but in such a way that, if the end_date is not in the same day as start_date, the view contain the entry again but with the start_date set to 00:00 of the next day.. (and so on, repeated as many times as needed until the start_date is in the same day as the end_date)
As an example:
if the activity table contains:
+--------------+----------------------------+----------------------------+
| id | start_date | end_date |
+--------------+----------------------------+----------------------------+
| 1 | 2014-12-02 14:12:00+00 | 2014-12-03 06:45:00+00 |
| 2 | 2014-12-05 15:25:00+00 | 2014-12-05 07:29:00+00 |
+--------------+----------------------------+----------------------------+
The view should contain:
+--------------+----------------------------+----------------------------+
| activity_id | start_date | end_date |
+--------------+----------------------------+----------------------------+
| 1 | 2014-12-02 14:12:00+00 | 2014-12-02 23:59:59+00 |
| 1 | 2014-12-03 00:00:00+00 | 2014-12-03 06:45:00+00 |
| 2 | 2014-12-05 15:25:00+00 | 2014-12-05 07:29:00+00 |
+--------------+----------------------------+----------------------------+
Any help would be greatly appreciated!
PS: I'm using postgresql