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
Select Specific Fields
To select specific fields from a measurement:
SELECT field1, field2 FROM measurement_name
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'
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'
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)
Filtering by Tag Values
To filter results based on tag values:
SELECT * FROM measurement_name
WHERE tag_key = 'tag_value'
Combining Filters
To combine multiple filter conditions using AND and OR:
SELECT * FROM measurement_name
WHERE tag_key = 'tag_value' AND field1 > 10
Order By Time
To order results by time (default is ascending order):
SELECT * FROM measurement_name
ORDER BY time DESC
Limit and Offset
To limit the number of returned results and offset:
SELECT * FROM measurement_name
LIMIT 10 OFFSET 5
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
Retention Policy
To specify a retention policy in a query:
SELECT * FROM "retention_policy_name"."measurement_name"
Drop Measurement
To delete a measurement:
DROP MEASUREMENT measurement_name
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)