first()
The first
aggregate allows you to get the value of one column as ordered by another. For example, first(temperature, time)
will return the earliest temperature value based on time within an aggregate group.
Required Arguments
Name | Description |
---|---|
value | The value to return (anyelement) |
time | The timestamp to use for comparison (TIMESTAMP/TIMESTAMPTZ or integer type) |
Sample Usage
Get the earliest temperature by device_id:
SELECT device_id, first(temp, time)
FROM metrics
GROUP BY device_id;
WARNING:The
last
andfirst
commands do not use indexes, and instead perform a sequential scan through their groups. They are primarily used for ordered selection within aGROUP BY
aggregate, and not as an alternative to anORDER BY time DESC LIMIT 1
clause to find the latest value (which will use indexes).
当前内容版权归 TimescaleDB 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 TimescaleDB .