DEV Community

Lourdes Suello
Lourdes Suello

Posted on

Query sample in Influxdb

InfluxDB is a time-series database that uses a SQL-like query language called InfluxQL. Below are examples of common queries in InfluxDB:

Basic Select Query
To retrieve data from a measurement:

SELECT * FROM measurement_name
Enter fullscreen mode Exit fullscreen mode

Select Specific Fields
To select specific fields from a measurement:

SELECT field1, field2 FROM measurement_name
Enter fullscreen mode Exit fullscreen mode

Time Range Query
To retrieve data within a specific time range:

SELECT * FROM measurement_name
WHERE time >= '2022-01-01T00:00:00Z' AND time <= '2022-01-02T00:00:00Z'
Enter fullscreen mode Exit fullscreen mode

Aggregation Query
To perform aggregation operations like mean, sum, max, etc.:

SELECT MEAN(field1) FROM measurement_name
WHERE time >= '2022-01-01T00:00:00Z' AND time <= '2022-01-02T00:00:00Z'
Enter fullscreen mode Exit fullscreen mode

Group By Time Interval
To group results by a specified time interval:

SELECT MEAN(field1) FROM measurement_name
WHERE time >= '2022-01-01T00:00:00Z' AND time <= '2022-01-02T00:00:00Z'
GROUP BY time(1h)
Enter fullscreen mode Exit fullscreen mode

Filtering by Tag Values
To filter results based on tag values:

SELECT * FROM measurement_name
WHERE tag_key = 'tag_value'
Enter fullscreen mode Exit fullscreen mode

Combining Filters
To combine multiple filter conditions using AND and OR:

SELECT * FROM measurement_name
WHERE tag_key = 'tag_value' AND field1 > 10
Enter fullscreen mode Exit fullscreen mode

Order By Time
To order results by time (default is ascending order):

SELECT * FROM measurement_name
ORDER BY time DESC
Enter fullscreen mode Exit fullscreen mode

Limit and Offset
To limit the number of returned results and offset:

SELECT * FROM measurement_name
LIMIT 10 OFFSET 5
Enter fullscreen mode Exit fullscreen mode

Continuous Query
To create a continuous query for downsampling data:

CREATE CONTINUOUS QUERY cq_name ON db_name
BEGIN
  SELECT MEAN(field1) INTO downsampled_measurement
  FROM measurement_name
  GROUP BY time(1h)
END
Enter fullscreen mode Exit fullscreen mode

Retention Policy
To specify a retention policy in a query:

SELECT * FROM "retention_policy_name"."measurement_name"
Enter fullscreen mode Exit fullscreen mode

Drop Measurement
To delete a measurement:

DROP MEASUREMENT measurement_name
Enter fullscreen mode Exit fullscreen mode

These examples provide a basic understanding of how to query InfluxDB using InfluxQL. For more complex queries and functions, refer to the official InfluxDB documentation.

Top comments (0)