Supposed I have a user travelling table like this:
id | start_time | end_time | location |
----+--------------+------------+------------+
1 | 1 | 2 | Loc_A |
1 | 3 | 4 | Loc_B |
1 | 5 | 6 | Loc_A |
1 | 7 | 8 | Loc_C |
1 | 9 | 10 | Loc_A |
1 | 13 | 15 | Loc_B |
1 | 16 | 17 | Loc_C |
2 | 1 | 3 | Loc_D |
Supposedly we are using "Loc_B" as the reference point – the point that the user must pass. We will have 2 nodes @ "Loc_B". Left tree contains all the rows before the current reference row, right tree contains all the rows after the current reference row (i.e Loc_B @ row 2) till the next reference row (i.e Loc_B @ row 6).
For the 1st node (row number 2):
left tree [ Loc_A ]
right tree [ Loc_A, Loc_C, Loc_A ].
For the 2nd node (row number 2),
left tree [ Loc_A, Loc_C, Loc_A ]
right tree [ Loc_C ].
Time diff is calculated as follow: if row is in left tree, then reference row's start time minus the inspecting row's end time. The sign of value will be minus. If row is in the right tree, then diff is equal to the inspecting row's start time minus the reference row's end time.
Time diff value for 1st node will be:
left_tree [ -1],
right_tree [ +1, +3, +5].
Time diff value for 2nd node will be:
left_tree [ -7, -5, -3 ],
right_tree [ +1 ].
Then the final diff value column, we have to compare 2 consecutive nodes with each other in pair and compare the node[0]'s right tree with node[1] 's left tree. Taking the absolute minimum value and append the sign based on its original value (or whether it is in left or right tree)
For user id – 2, diff will be NIL since he didn't pass through the reference point (Loc_B)
id | start_time | end_time | location | diff |
----+--------------+------------+------------+---------
1 | 1 | 2 | Loc_A | -1
1 | 3 | 4 | Loc_B | N/A
1 | 5 | 6 | Loc_A | +1
1 | 7 | 8 | Loc_C | +3
1 | 9 | 10 | Loc_A | -3
1 | 13 | 15 | Loc_B | N/A
1 | 16 | 17 | Loc_C | +1
2 | 1 | 3 | Loc_D | NIL
Other note: left_tree and right_tree can be empty in case of Loc_B is the first or last record (partition by id).
May I know whether it is possible to do this?
Best Answer
Produces your result exactly.
1000
is just some "higher value than any other". Since your actual problem seems to operate with times and intervalsinfinity
would be the perfect choice.Related answer with detailed explanation how groups (
right_tree
andleft_tree
here) are formed:Select longest continuous sequence
SQL Fiddle.