Documentation

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

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 > 10

Subtraction

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 <= 3

Multiplication

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 >= 20

Multiplication distributes across other operators.

SELECT 10 * (A + B + C) FROM example

SELECT 10 * (A - B - C) FROM example

SELECT 10 * (A + B - C) FROM example

Division

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 <= 2

Division distributes across other operators.

SELECT 10 / (A + B + C) FROM example

SELECT 10 / (A - B - C) FROM example

SELECT 10 / (A + B - C) FROM example

Modulo

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 = 0

Bitwise 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 > 0

Bitwise 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 = 12

Bitwise 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 > 0

Unsupported 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 example

For 16-bit data:

SELECT A ^ 65535 FROM example

For 32-bit data:

SELECT A ^ 4294967295 FROM example

In 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: * + 2

Perform a mathematical operation on a wildcard within a function.

SELECT COUNT(*) / 2 FROM "nope"
-- ERR: unsupported expression with wildcard: count(*) / 2

Perform a mathematical operation on a regular expression.

SELECT /A/ + 2 FROM "nope"
-- ERR: error parsing query: found +, expected FROM at line 1, char 12

Perform a mathematical operation on a regular expression within a function.

SELECT COUNT(/A/) + 2 FROM "nope"
-- ERR: unsupported expression with regex field: count(/A/) + 2

Mathematical 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!


The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Read more

InfluxDB v3 enhancements and InfluxDB Clustered is now generally available

New capabilities, including faster query performance and management tooling advance the InfluxDB v3 product line. InfluxDB Clustered is now generally available.

InfluxDB v3 performance and features

The InfluxDB v3 product line has seen significant enhancements in query performance and has made new management tooling available. These enhancements include an operational dashboard to monitor the health of your InfluxDB cluster, single sign-on (SSO) support in InfluxDB Cloud Dedicated, and new management APIs for tokens and databases.

Learn about the new v3 enhancements


InfluxDB Clustered general availability

InfluxDB Clustered is now generally available and gives you the power of InfluxDB v3 in your self-managed stack.

Talk to us about InfluxDB Clustered

InfluxDB Cloud Serverless