I have a table with a list of registers where each one represents an event, I must group this registers that have happened in the same day. Also I must group the events in the day, grouping by id_fertilizer and calibrado, but I can't group the first result with the last ones.
my SQL result is something like this:
work_date id_fertilizer calibrado area begin_date end_date
[1] '2014-07-22' 43 NULL 0 "07:03:42.0" "07:08:00.0"
[2] '2014-07-22' 49 NULL 0 "07:08:52.0" "07:44:04.0"
[3] '2014-07-22' 49 true 54101 "07:49:41.0" "12:00:05.0"
[4] '2014-07-22' 49 true 4893 "12:00:30.0" "14:06:13.0"
[5] '2014-07-22' 43 NULL 0 "14:06:51.0" "14:49:30.0"
[6] '2014-07-22' 43 NULL 12397 "14:50:04.0" "16:12:03"
where I must group the lines 3 and 4 in one line and the lines 5 and 6 in another one, taking the min() begin_date and max() end_date and sum() the area
lines 1 and 2 are independent lines.
At the end I must have 4 lines: 1, 2, (3+4), (5+6)
The result I've got is something like this (using the function window), but it's wrong, because it have grouped the lines 1,5,6 in one line:
work_date id_fertilizer calibrado area begin_date end_date
"2014-07-22 00:00:00.0" 43 NULL 1 "07:03:42.0" "16:12:03.0"
"2014-07-22 00:00:00.0" 49 NULL 0 "07:08:52.0" "07:44:04.0"
"2014-07-22 00:00:00.0" 49 true 5 "07:49:41.0" "14:06:13.0"
My result, with 3 lines says the guys worked from 07:03:42 to 16:12:03 with id_fertilizer 43, but also says he worked from 07:08:52 07:44:04 with id_fertilizer 49. It does not make sense, I must respect the chronological order of the events. So, the result I am expecting is this:
work_date id_fertilizer calibrado area begin_date end_date
[1] '2014-07-22' 43 NULL 0 "07:03:42.0" "07:08:00.0"
[2] '2014-07-22' 49 NULL 0 "07:08:52.0" "07:44:04.0"
[3] '2014-07-22' 49 true 58994 "07:49:41.0" "14:06:13.0"
[4] '2014-07-22' 43 NULL 12397 "14:06:51.0" "16:12:03"