wizzfizz94
wizzfizz94

Reputation: 1556

Round negative values to zero in influx query?

I'm trying to set a lower bounds of zero on my influx query result so that negative values are replaced with zero in the result. e.g. For the query:

SELECT x from measurement

If my raw response is

time                x
----                -
1632972969471900180 0
1632972969471988621 -130
1632972969472238055 803

then i want to alter the query so that the result is:

time                x'
----                -
1632972969471900180 0
1632972969471988621 0
1632972969472238055 803

Upvotes: 0

Views: 1759

Answers (1)

wizzfizz94
wizzfizz94

Reputation: 1556

My solution was to use the ABS absolute value function, adding the absolute value to the original value and dividing by 2. This maps negative values to zero and leaves posiive (and zero) values unchanged. e.g.

SELECT (x + ABS(x)) / 2 from measurement
time                x                        x'
----                -                        -
1632972969471900180 0     = 0 + 0 / 2      = 0
1632972969471988621 -130  = -130 + 130 / 2 = 0
1632972969472238055 803   = 803 + 803 / 2  = 803

Upvotes: 2

Related Questions