qSQL · Grouping & Aggregation · Practice Question
xbar: Count Rows in Time Buckets
Question
Given the following table of market data:
t:([] sym:`A`A`A`A`B`B`B`C`C`C;
time:2026.06.11D00:01:00 2026.06.11D00:07:00 2026.06.11D00:18:00 2026.06.11D00:31:00
2026.06.11D00:04:00 2026.06.11D00:16:00 2026.06.11D00:44:00
2026.06.11D00:02:00 2026.06.11D00:29:00 2026.06.11D00:47:00;
price:101.2 102.5 103.1 104.0 99.8 100.4 101.0 200.1 201.5 202.0)Write a qSQL query to return the number of rows per 15-minute interval, per symbol.
Solution
select numRows:count i by sym,timeBucket:15 xbar time.minute from tResult:
sym timeBucket| numRows
--------------| -------
A 00:00 | 2
A 00:15 | 1
A 00:30 | 1
B 00:00 | 1
B 00:15 | 1
B 00:30 | 1
C 00:00 | 1
C 00:15 | 1
C 00:45 | 1Alternative Solution
Using timestamp buckets directly:
select numRows:count i by sym,timeBucket:00D00:15 xbar time from tResult:
sym timeBucket | numRows
---------------------------------| -------
A 2026.06.11D00:00:00.000000000| 2
A 2026.06.11D00:15:00.000000000| 1
A 2026.06.11D00:30:00.000000000| 1
B 2026.06.11D00:00:00.000000000| 1
B 2026.06.11D00:15:00.000000000| 1
B 2026.06.11D00:30:00.000000000| 1
C 2026.06.11D00:00:00.000000000| 1
C 2026.06.11D00:15:00.000000000| 1
C 2026.06.11D00:45:00.000000000| 1Another equivalent timestamp-based form is:
select numRows:count i by sym,timeBucket:(60*15*1000000000) xbar time from tExplanation
The goal is to group records by:
sym- A 15-minute time bucket
The expression:
time.minuteconverts each timestamp to its time of day as a minute value (hours and minutes, hh:mm), dropping the date and seconds.
For example:
`minute$2026.06.11D00:18:00returns:
00:18(time.minute is the dot-notation form of the same cast, applied to the column.)
The expression:
15 xbar time.minuterounds each minute down to the nearest 15-minute boundary.
For example:
15 xbar 00:18returns:
00:15and:
15 xbar 00:47returns:
00:45So the query:
select numRows:count i by sym,timeBucket:15 xbar time.minute from tgroups the table by symbol and 15-minute bucket, then counts the number of rows in each group.
Timestamp Buckets
If you want the bucket to remain a full timestamp, use:
00D00:15 xbar timeFor example:
00D00:15 xbar 2026.06.11D00:18:00.000000000returns:
2026.06.11D00:15:00.000000000This is useful when the date needs to be preserved.
The same interval can also be written as a number of nanoseconds, 60*15*1000000000, so this query is equivalent:
select numRows:count i by sym,timeBucket:(60*15*1000000000) xbar time from tSummary
Use:
15 xbar time.minutewhen you only need the time-of-day bucket.
Use:
00D00:15 xbar timewhen you want to preserve the full timestamp, including the date.
Both approaches are common when aggregating market data into fixed time intervals.