Calculate percentages in queries

Updated at:
Copy as MD

InfluxQL supports basic math operators in SELECT statements, so you can calculate percentages directly from field values.

Basic math operators

SELECT statements support +, -, *, /, and (). Use these operators to perform inline calculations across field keys.

-- Add two field keys
SELECT field_key1 + field_key2 AS "field_key_sum"
FROM "measurement_name"
WHERE time < now() - 15m

-- Subtract one field key from another
SELECT field_key1 - field_key2 AS "field_key_difference"
FROM "measurement_name"
WHERE time < now() - 15m

-- Group and chain calculations
SELECT (field_key1 + field_key2) - (field_key3 + field_key4) AS "some_calculation"
FROM "measurement_name"
WHERE time < now() - 15m

Calculate a percentage

Divide one field by another and multiply by 100:

SELECT (field_key1 / field_key2) * 100 AS "calculated_percentage"
FROM "measurement_name"
WHERE time < now() - 15m

Calculate a percentage using aggregate functions

Before writing the query, note these requirements:

  1. All fields must use aggregate functions. Mixing aggregated and non-aggregated fields in the same query is not valid.

  2. Every aggregate function requires a GROUP BY time() clause to define the time interval for grouping data points.

  3. Specify a time range in the WHERE clause. Without one, the aggregate functions may consume a large number of compute resources.

SELECT (sum(field_key1) / sum(field_key2)) * 100 AS "calculated_percentage"
FROM "measurement_name"
WHERE time < now() - 15m
GROUP BY time(1m)

Examples

The following examples use sample Apple Stand data that tracks the weight of baskets containing different apple varieties throughout the day.

Set up the sample data

  1. Download the sample data.

  2. Import the sample data:

    influx -ssl -username <Username> -password <Password> -host <Domain name> -port 3242 -import -path=path/to/apple_stand.txt -database=apple_stand

Calculate the percentage of total weight per apple variety

The following query calculates each apple variety's share of the total weight at each data point:

SELECT
    ("braeburn" / total_weight) * 100,
    ("granny_smith" / total_weight) * 100,
    ("golden_delicious" / total_weight) * 100,
    ("fuji" / total_weight) * 100,
    ("gala" / total_weight) * 100
FROM "apple_stand"."autogen"."variety"

Calculate the aggregate percentage per apple variety

The following query calculates each variety's share of the average total weight, grouped into 1-hour intervals:

SELECT
    (mean("braeburn") / mean(total_weight)) * 100,
    (mean("granny_smith") / mean(total_weight)) * 100,
    (mean("golden_delicious") / mean(total_weight)) * 100,
    (mean("fuji") / mean(total_weight)) * 100,
    (mean("gala") / mean(total_weight)) * 100
FROM "apple_stand"."autogen"."variety"
WHERE time >= '2018-06-18T12:00:00Z' AND time <= '2018-06-19T04:35:00Z'
GROUP BY time(1h)

All fields use mean() to satisfy the aggregate-only requirement. The GROUP BY time(1h) clause groups data into 1-hour blocks, and the WHERE time range limits the scan to a specific window.