By default, TSDB for InfluxDB® returns results based on timestamps in ascending order. The first returned point is attached with the earliest timestamp and the last returned point is attached with the latest timestamp. ORDER BY time DESC
reverses the default timestamp order so that TSDB for InfluxDB® returns the points that are attached with the latest timestamp.
Syntax
SELECT_clause [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] ORDER BY time DESC
Description
If a query includes a GROUP BY
clause, the ORDER BY time DESC
clause must appear after the GROUP BY
clause. If a query includes a WHERE
clause and no GROUP BY
clause, the ORDER BY time DESC
clause must appear after the WHERE
clause.
Examples
Obtain points based on timestamps in descending order
> SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica' ORDER BY time DESC
name: h2o_feet
time water_level
---- -----------
2015-09-18T21:42:00Z 4.938
2015-09-18T21:36:00Z 5.066
[...]
2015-08-18T00:06:00Z 2.116
2015-08-18T00:00:00Z 2.064
The query returns the points from the h2o_feet
measurement based on timestamps in descending order. The first returned point is attached with the latest timestamp. If the preceding statement does not include ORDER by time DESC
, the query returns the points based on timestamps in ascending order. The first returned point is attached with the earliest timestamp 2015-08-18T00:00:00Z
. The last returned point is attached with the latest timestamp 2015-09-18T21:42:00Z
.
Obtain points based on timestamps in descending order and include a GROUP BY time()
clause
> SELECT MEAN("water_level") FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:42:00Z' GROUP BY time(12m) ORDER BY time DESC
name: h2o_feet
time mean
---- ----
2015-08-18T00:36:00Z 4.6825
2015-08-18T00:24:00Z 4.80675
2015-08-18T00:12:00Z 4.950749999999999
2015-08-18T00:00:00Z 5.07625
The query uses an InfluxQL function and a time interval specified by the GROUP BY
clause to calculate the average field value of the water_level
field key for each 12-minute interval in the specified time range. The first result returned by the ORDER BY time DESC
clause corresponds to the latest 12-minute interval. If the preceding statement does not include ORDER by time DESC
, the query returns the points based on timestamps in ascending order. The first returned point is attached with the earliest timestamp 2015-08-18T00:00:00Z
. The last returned point is attached with the latest timestamp 2015-08-18T00:36:00Z
.
InfluxDB® is a trademark registered by InfluxData, which is not affiliated with, and does not endorse, TSDB for InfluxDB®.