We have a project where data is written to a logging table. Now, when investigating a problem, it would be useful to query for the problem row, and also get the surrounding rows, so that we can easily see what led up to the problem. The log table had a timestamp field, so we can order by that.
Essentially I want something like "grep -C" for SQL.
As an example, imagine we have a reference_id column, and a activity_code column. I find a problem with reference ID = 1234, so I want to find the preceding N activities.
Edit: Some example data
Code Reference Time
Allocate ABC1 9:00
Allocate ABC2 9:01
Problem MYREF1 9:02
Allocate ABC3 9:03
Allocate ABC4 9:03
Problem2 MYREF1 9:04
Allocate ABC5 9:09
I'm given "MYREF1" as something to look at, but I want to see what's been going on at the same kind of time. I want a query that will pick up the "MYREF1" reference rows, as well as some of the other rows (maybe 1 or 2 of the surrounding or preceding rows). In my example that would be ABC2 and ABC4 if I wanted the preceding rows (analogous to grep -B1)