InfluxQL math operators
Use InfluxQL mathematical operators to perform mathematical operations in InfluxQL queries.
Mathematical operators follow the standard order of operations.
Parentheses take precedence to division and multiplication, which takes precedence to addition and subtraction.
For example 5 / 2 + 3 * 2 = (5 / 2) + (3 * 2) and 5 + 2 * 3 - 2 = 5 + (2 * 3) - 2.
- Addition
- Subtraction
- Multiplication
- Division
- Modulo
- Bitwise AND
- Bitwise OR
- Bitwise Exclusive-OR
- Unsupported Operators
- Notable behaviors of mathematical operators
Addition
Add two numeric operands. Operands may be an identifier, constant, or literal numeric value.
SELECT A + 5 FROM example
SELECT A + B FROM example
SELECT * FROM example WHERE A + 5 > 10
SELECT * FROM example WHERE A + B > 10Subtraction
Subtract one numeric operand from another. Operands may be an identifier, constant, or literal numeric value.
SELECT 1 - A FROM example
SELECT B - A FROM example
SELECT * FROM example WHERE 1 - A <= 3
SELECT * FROM example WHERE B - A <= 3Multiplication
Multiply two numeric operands. Operands may be an identifier, constant, or literal numeric value.
SELECT A * 10 FROM example
SELECT A * B FROM example
SELECT * FROM example WHERE A * 10 >= 20
SELECT * FROM example WHERE A * B >= 20Multiplication distributes across other operators.
SELECT 10 * (A + B + C) FROM example
SELECT 10 * (A - B - C) FROM example
SELECT 10 * (A + B - C) FROM exampleDivision
Divide one numeric operand by another. Operands may be an identifier, constant, or literal numeric value.
SELECT A / 10 FROM example
SELECT A / B FROM example
SELECT * FROM example WHERE A / 10 <= 2
SELECT * FROM example WHERE A / B <= 2Division distributes across other operators.
SELECT 10 / (A + B + C) FROM example
SELECT 10 / (A - B - C) FROM example
SELECT 10 / (A + B - C) FROM exampleModulo
Perform a modulo operation with two numeric operands. Operands may be an identifier, constant, or literal numeric value.
SELECT A % 2 FROM example
SELECT A % B FROM example
SELECT A FROM example WHERE A % 2 = 0
SELECT A, B FROM example WHERE A % B = 0Bitwise AND
Perform a bitwise AND operation on two operands of the same type.
Supported types are integers and booleans.
Operands may be an identifier, constant, literal integer value, or literal boolean value.
SELECT A & 255 FROM example
SELECT A & B FROM example
SELECT (A ^ true) & B FROM example
SELECT * FROM example WHERE A & 15 > 0Bitwise OR
Perform a bitwise OR operation on two operands of the same type.
Supported types are integers and booleans.
Operands may be an identifier, constant, literal integer value, or literal boolean value.
SELECT A | 5 FROM example
SELECT A | B FROM example
SELECT * FROM example WHERE "bitfield" | 12 = 12Bitwise Exclusive-OR
Perform a bitwise Exclusive-OR operation on two operands of the same type.
Supported types are integers and booleans.
Operands may be an identifier, constant, literal integer value, or literal boolean value.
SELECT A ^ 255 FROM example
SELECT A ^ B FROM example
SELECT * FROM example WHERE "bitfield" ^ 6 > 0Unsupported Operators
Inequalities
Using any of =,!=,<,>,<=,>=,<> in the SELECT statement yields
empty results for all types.
Comparison operators can only be used in the WHERE clause.
Logical Operators
Using any of !|,NAND,XOR,NOR yield a parser error.
Additionally using AND, OR in the SELECT clause of a query will not behave
as mathematical operators and simply yield empty results, as they are InfluxQL tokens.
However, you can apply the bitwise operators &, | and ^ to boolean values.
Bitwise Not
There is no bitwise-not operator, because the results you expect depend on the width of your bitfield. InfluxQL does not know how wide your bitfield is, so cannot implement a suitable bitwise-not operator.
For example, if your bitfield is 8 bits wide, then the integer 1 represents the bits 0000 0001.
The bitwise-not of this should return the bits 1111 1110 (that is, the integer 254)
However, if your bitfield is 16 bits wide, then the integer 1 represents the bits 0000 0000 0000 0001.
The bitwise-not of this should return the bits 1111 1111 1111 1110 (that is, the integer 65534)
Solution
You can implement a bitwise-not operation by using the ^ (bitwise xor) operator
together with the number representing all-ones for your word-width:
For 8-bit data:
SELECT A ^ 255 FROM exampleFor 16-bit data:
SELECT A ^ 65535 FROM exampleFor 32-bit data:
SELECT A ^ 4294967295 FROM exampleIn each case, the constant you need can be calculated as (2 ** width) - 1.
Notable behaviors of mathematical operators
Mathematical operators with wildcards and regular expressions
InfluxQL does not support combining mathematical operations with a wildcard (*)
or regular expression
in the SELECT clause.
The following queries are invalid and the output is an error:
Perform a mathematical operation on a wildcard.
SELECT * + 2 FROM "nope"
-- ERR: unsupported expression with wildcard: * + 2Perform a mathematical operation on a wildcard within a function.
SELECT COUNT(*) / 2 FROM "nope"
-- ERR: unsupported expression with wildcard: count(*) / 2Perform a mathematical operation on a regular expression.
SELECT /A/ + 2 FROM "nope"
-- ERR: error parsing query: found +, expected FROM at line 1, char 12Perform a mathematical operation on a regular expression within a function.
SELECT COUNT(/A/) + 2 FROM "nope"
-- ERR: unsupported expression with regex field: count(/A/) + 2Mathematical operators with functions
InfluxQL does not support mathematical operators inside of function calls.
Note that InfluxQL only allows functions in the SELECT clause.
For example, the following will work:
SELECT 10 * mean("value") FROM "cpu"However, the following query will return a parse error:
SELECT mean(10 * "value") FROM "cpu"
-- Error: expected field argument in mean()InfluxQL supports subqueries which offer similar functionality to using mathematical operators inside a function call.
Was this page helpful?
Thank you for your feedback!
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for InfluxDB Cloud Serverless and this documentation. To find support, use the following resources:
Customers with an annual or support contract can contact InfluxData Support.