As far as I know, this isn’t possible using InfluxQL. Even in Flux, it’s a little tricky but totally possible.

If you’re open to using Flux, you can enable and use Flux with InfluxDB 1.7.

Calculating the time between points is simple with the `elapsed()`

function. It appends an `elapsed`

column containing the elapsed time (in the specified unit) between the current point and the previous point. You can then filter rows out with `elapsed`

values great than 5 minutes. This will give you all the `gap_end`

points. To get the `gap_start`

points, you read the same data but in reverse order (by time).

The example below does just that. It creates two sets of data, `gapStarts`

and `gapEnds`

. For each, it uses the `elapsed()`

function to calculate the time between points. It then filters out records with `elapsed`

values that exceed the 5 minute threshold (note that elapsed values in `gapStarts`

are negative because rows are ordered by time in reverse). Each then tags the rows based on which gap end they represent. It then merges the two streams of data together with the `union()`

function and sorts by time again.

```
baseData = from(bucket: "default")
|> range(start: -2h)
|> filter(fn: (r) => r._measurement == "exampleMeasurement" and r._field == "exampleField")
gapStarts = baseData
|> sort(columns: ["_time"], desc: true)
|> elapsed(unit: 1m)
|> filter(fn: (r) => r.elapsed <= -5)
|> map(fn: (r) => ({ r with gapSide: "gap_start" }))
gapEnds = baseData
|> elapsed(unit: 1m)
|> filter(fn: (r) => r.elapsed >= 5)
|> map(fn: (r) => ({ r with gapSide: "gap_end" }))
union(tables: [gapStarts, gapEnds])
|> keep(columns: ["_time", "gapSide"])
|> sort(columns: ["_time"])
```