On any timerange given, which i assume I have to compute using the timestamp_sequence function, i want to know the number of alive sessions in that time. An alive session is a session which started (startTime) before or in the given timestamp and ended (endTime) after the given timestamp.
SELECT
t.ts,
count(s.sessionId)
FROM (
SELECT timestamp_sequence(
date_trunc('minute', dateadd('h', -6, now())),
60000000L
) AS ts
FROM long_sequence(60)
) t
LEFT JOIN Sessions s
ON s.startTime <= t.ts AND s.endTime > t.ts
ORDER BY ts;
While the inner query seems to correctly return each minute from an hour ago until now, when running the entire query i get some strange timestamp. What am I doing wrong?
You can get the dense range of time intervals with this
select startTime, first(startTime) from Sessions
sample by 1m from '2025-06-16T01:15' to '2025-06-16T04:15'
Note I am adding a cheap aggregation (first), as otherwise SAMPLE BY will complain.
So your query would become
SELECT
t.ts,
count(s.sessionId)
FROM (
select startTime ts, first(startTime) from Sessions
sample by 1m from '2025-06-16T01:15' to '2025-06-16T04:15'
) t
LEFT JOIN Sessions s
ON s.startTime <= t.ts AND s.endTime > t.ts
ORDER BY ts;
Note you could also use a dynamic date, as you were doing before
SELECT
t.ts,
count(s.sessionId)
FROM (
select startTime ts, first(startTime) from Sessions
sample by 1m
from
date_trunc('minute',dateadd('h', -6, now()))
to
date_trunc('minute',now())
) t
LEFT JOIN Sessions s
ON s.startTime <= t.ts AND s.endTime > t.ts
ORDER BY ts;
I’ve tried the both the solutions. I get the error FROM-TO intervals are not supported for keyed SAMPLE BY queries when performing any query like @javier suggested.
In the end I’ve done something like the following, getting the correct data:
SELECT
t.ts,
count(s.sessionId)
FROM (
SELECT (dateadd('m', -60, now()) + (x * 6_000_000_0L)) as ts FROM (
SELECT x - 1 as x FROM long_sequence(61)
)
ORDER BY ts
) t
LEFT JOIN Sessions s
ON s.startTime <= t.ts AND s.endTime > t.ts
ORDER BY ts;
Now for the next step: I need to include this in a Grafana graph, letting the user decide the range of time to visualize. How should I update the query to get the range from Grafana?
In that case, you can add a WHERE clause and pass $__timefilter or $timeFrom + $timeTo. This will be populated by Grafana based on the time filter button.
I think we will add generate_series to make this timestamp generation more ergonomic in future. Look out for it!