Search code examples
sqloracle-database

Orcale SQL select next and previous 3 rows of a specific row in groups


I have a dataset of bus stop sequence, I would like to select the last 3 rows (last 3 bus stops) and next 3 rows (next 3 bus stops) of a specific bus stop including the specific bus stop. This bus stop is unique in each set group.

For example, from this table, I would like to get the last and next 3 rows of bus top XXX.

Group Bus_Stop Sequence
A 518 2
A 564 3
A 513 4
A 698 5
A XXX 6
A 987 7
A 564 8
A 845 9
A 365 10
B 518 14
B 564 15
B 513 16
B 698 17
B XXX 18
B 658 19
B 234 20
B 122 21
B 456 22

The result should look like this:

Group Bus_Stop Sequence
A 564 3
A 513 4
A 698 5
A XXX 6
A 987 7
A 564 8
A 845 9
B 564 15
B 513 16
B 698 17
B XXX 18
B 658 19
B 234 20
B 122 21

I tried to use below sub query but it keeps running so I had to kill the process:

select Group,Bus_Stop,Sequence from table t
where t.Sequence-3<=(
select Sequence from table
where Bus_Stop = XXX and Group = t.Group)
and t.Sequence+3 >=(
select Sequence from table
where Bus_Stop = XXX and Group = t.Group)

Solution

  • You can self join the tables and filter the XXX stop and then just compare the sequence between +3 and -3

    SELECT t1.groups, t1.bus_stop, t1.sequence
    FROM test t1
    INNER JOIN test t2 ON t1.groups = t2.groups
      AND t1.sequence BETWEEN t2.sequence - 3 AND t2.sequence + 3
    WHERE t2.bus_stop = 'XXX'
    ORDER BY t1.groups, t1.sequence;
    

    Example Fiddle

    Output

    GROUPS BUS_STOP SEQUENCE
    A 564 3
    A 513 4
    A 698 5
    A XXX 6
    A 987 7
    A 564 8
    A 845 9
    B 564 15
    B 513 16
    B 698 17
    B XXX 18
    B 658 19
    B 234 20
    B 122 21