documentation/query/functions/timestamp.md
timestamp(columnName) elects a
designated timestamp:
dynamic timestamp)timestamp column:::note
Checking if tables contain a designated timestamp column can be done via the
tables() and table_columns() functions which are described in the
meta functions documentation page.
There are two timestamp resolutions available in QuestDB: microseconds and nanoseconds. See Timestamp resolution for more details. :::
Create a designated timestamp column during table creation. For more information, refer to the CREATE TABLE section.
Creates a designated timestamp column in
the result of a query. Assigning a timestamp in a SELECT statement
(dynamic timestamp) allows for time series operations such as LATEST BY,
SAMPLE BY or LATEST BY on tables which do not have a designated timestamp
assigned.
When filtering on a designated timestamp column in WHERE clauses, QuestDB automatically optimizes the query by applying time-based partition filtering. This optimization also works with subqueries that return timestamp values.
For example:
SELECT *
FROM trades
WHERE timestamp > (SELECT min(timestamp) FROM trades)
AND timestamp < (SELECT max(timestamp) FROM trades);
In this case, if ts is the designated timestamp column, QuestDB will optimize the query by:
This optimization applies to timestamp comparisons using:
>)<)=)>=)<=)The following creates a table with designated timestamp.
CREATE TABLE
temperatures(ts timestamp, sensorID symbol, sensorLocation symbol, reading double)
timestamp(ts);
The following will query a table and assign a
designated timestamp to the output. Note
the use of brackets to ensure the timestamp clause is applied to the result of
the query instead of the whole trades table.
(SELECT cast(dateTime AS TIMESTAMP) ts, symbol, price FROM trades) timestamp(ts);
Although the trades table does not have a designated timestamp, we are able
to create one on the fly. Now, we can use this into a subquery to perform
timestamp operations.
SELECT ts, avg(price) FROM
(SELECT cast(dateTime AS TIMESTAMP) ts, price FROM trades) timestamp(ts)
SAMPLE BY 1d;
If the data is unordered, it is important to order it first.
SELECT ts, avg(price) FROM
(SELECT ts, price FROM unordered_trades ORDER BY ts) timestamp(ts)
SAMPLE BY 1d;