I have a table in the ORACLE database, details below:
--------------------------------------------
| FRUITS |
--------------------------------------------
| FRUIT_NAME | GROWTH_TIME | GROWTH_PLACE |
--------------------------------------------
| melon | 0600 | shelf1 |
| melon | 0630 | shelf1 |
| melon | 0700 | shelf1 |
| melon | 0730 | shelf1 |
| melon | 0800 | shelf1 |
| orange | 0600 | shelf5 |
| orange | 0630 | shelf5 |
| orange | 0700 | shelf5 |
| orange | 0730 | shelf5 |
| orange | 0800 | shelf5 |
| orange | 0830 | shelf5 |
| orange | 0900 | shelf5 |
| orange | 0930 | shelf5 |
| orange | 1000 | shelf5 |
| orange | 1200 | shelf5 |
| orange | 1230 | shelf5 |
| orange | 1300 | shelf5 |
| orange | 1330 | shelf5 |
| orange | 1400 | shelf5 |
| apple | 0600 | shelf3 |
| apple | 0630 | shelf3 |
| apple | 0700 | shelf3 |
| apple | 0730 | shelf3 |
| apple | 0800 | shelf3 |
--------------------------------------------
and I would like to get results like these below:
--------------------------------------------
| FRUIT_NAME | GROWTH_TIME | GROWTH_PLACE |
--------------------------------------------
| melon | 0600-0800 | shelf1 |
| orange | 0600-1000 | shelf5 |
| orange | 1200-1400 | shelf5 |
| apple | 0600-0800 | shelf3 |
or like these:
-------------------------------------------------------------------
| FRUIT_NAME | GROWTH_START_TIME | GROWTH_END_TIME | GROWTH_PLACE |
-------------------------------------------------------------------
| melon | 0600 | 0800 | shelf1 |
| orange | 0600 | 1000 | shelf5 |
| orange | 1200 | 1400 | shelf5 |
| apple | 0600 | 0800 | shelf3 |
There is a small gap in the ORANGE case (between 1000 and 1400) and this is still the same shelf but with a small gap in time. It happens but I don’t know how to solve this problem.
you can solve this by analytics:
i.e. first we break the result set into groups where a group is defined as contigious dates for a given fruut/shelf.
We do this by checking the prior date and seeing if its < the current rows date – 30 minutes with
from this we can derive groups where the prior row was over 30 minutes older that this row:
now we just assign the group to each row with a max() analytic:
now all that’s left was the final group by on the
GRPto get the final answer.