Subtract two count values with different where clause in influxDb - influxdb

I have a measurement in influxDb with two keys: operation and count. The operation key can store two different values: 'add' and 'delete'.
I want to subtract the sum(count) value when operation='delete' to sum(count) value when operation='add'.
The following query is supported in mysql but it throws and error in influxql:
select (select sum(count) from measurement where operation='add') - (select sum(count) from measurement where operation='delete');
How can this be done using a single influxql query ? I don't think influxql allows two different where clauses in this case.

InfluxQL doesn't support this kind of multiquery math. You will need to calculate it on the app level.

Related

Solr pass value between subquery and upper query

I have to perform the following SQL query on a Solr collection:
select c.CONTRACT
from contracts c
where c.LIMIT_TYPE = 4
and c.TRANSACTION_DATE_DB = (select max(TRANSACTION_DATE_DB) from contracts c2 where c.CONTRACT = c2.CONTRACT and c2.LIMIT_TYPE = 4)
and c.STATUS = 'ENABLED'
group by c.CONTRACT
order by c.CONTRACT desc;
The objective of the query is the following:
the subquery find, for each contract, the record with last transaction date for limit type 4
the upper query checks if, for each contract, if the record returned has status enabled.
I've seen that there are two possible ways to perform this query using solr: join and subquery
Here below I'll paste my 100th attempt using subquery:
http://localhost:8983/solr/contracts_collection/select?
q=*:*
&indent=true
fq=LIMIT_TYPE:4&
fq=STATUS:ENABLED
&fl=*,mySub:[subquery]
&mySub.q=*
&mySub.fq={!term f=CONTRACT_IDENTITY v=$row.CONTRACT_IDENTITY}
&mySub.fq=LIMIT_TYPE:4
&mySub.fl=CONTRACT_IDENTITY,TRANSACTION_DATE_DB,LIMIT_TYPE
&mySub.sort=TRANSACTION_DATE_DB+desc
&mySub.rows=1
The subquery is correct. However, for each contract, the result contains all the records for that contract. In fact, I'm missing here how to make the upper query return, for each contract, only the record having the same transaction_date_db returned by the subquery.
I tried to add the following condition:
&mySub.fq={!term f=TRANSACTION_DATE_DB v=$row.TRANSACTION_DATE_DB}
But still, I don't have the desired result.
I've also saw join, but I've failed miserably :P
Does anyone have any suggestion? Going crazy for two days now.
Thanks a lot

Apply a SUM function on the product of two fields in InfluxDB

I have the following query:
SELECT sum("field1" * "field2") FROM "my_db"."autogen"."data" GROUP BY time(1d) FILL(null)
In short I would like to perform the operation sum on the product of two fields field and field2.
The above query returns an error: expected field argument in sum().
Is this kind of thing at all possible in InfluxDB?
Here's a idea: try Sub Query
Note:I don't have editor right now so it might give error too
SELECT SUM(Multiplication) FROM
(SELECT "field1" * "field2" as Multiplication, time(1d) as Days FROM
"my_db"."autogen"."data" GROUP BY time(1d) FILL(null)
) GROUP BY Days

InfluxDB - query with aggregation function DIFFERENCE() that returns value from other query

I have a atble in InfluxDB, which has some values. So what I wanted to do is that I wanted to calculate the difference changing with values. Here is my query:
SELECT * FROM (SELECT DIFFERENCE(value) FROM table) WHERE difference = 1;
And I wanted to select all values from the table, where detected difference is 1 an then since DIFFERENCE() function returns second value of difference between two values from the table I wanted to get this second value from the table, because I want to create a function for an anomaly that is detected between measurements A and B, where time(a) < time(b) and this function should return B.
Has anyone have an idea how can I do that with a single InfluxDB query?

SPSS - Rank and Partition

In SPSS Statistics Syntax File, I am looking to create a variable that calculates rank based on a desired partitioned column (e.g. equivalent to SQL "rank over (partition by column_a order by column b)" in Oracle SQL developer).
Please see the example:
Initial data without any filters:
Final output after applying get_rank:
To create a rank variable as described, first sort your data and then use the LAG function.
SORT CASES BY column_a column_b .
compute rank=1 .
IF ($CASENUM>1 AND column_a=LAG(column_a)) rank=LAG(rank) + 1 .
EXE .
LAG will look at the value of column_a for the prior case. In the syntax above it checks whether the value in column_a is different from that of the prior case.
If it has, then it will set the rank to 1. If it hasn't, then it will add 1 to the rank of the prior case. Just make sure your data is properly sorted first.
From there, if you want to look only at records that are rank=1, you can either use FILTER BY or SELECT IF to do that.
If indeed you only need the key to filter for key=1 then you can use this:
SORT CASES BY column_a column_b .
match files /file=* /by column_a /first=key1.
Now variable key1 will have value 1 for every first occurence of a column_a category, and you can use it to filter or select.
For a full ranking variable you can use this (don't even need to sort first):
RANK VARIABLES=b (A) BY a /RANK /TIES=MEAN.

InfluxDb Continuous Query that excludes zeros

I'm trying to create a continuous query in InfluxDb to downsample the measurement data to the hourly mean values. I can do that with the continuous query below.
CREATE CONTINUOUS QUERY "cq_test_1h" ON "db-name"
BEGIN
SELECT mean("value") AS "mean_value"
INTO "downsampled"."downsampled_measurement"
FROM "autogen"."measurement"
GROUP BY time(1h)
END
But I also want that if the hourly mean equals zero, the result is excluded; so the downsampled_measurement series does not contain any zero values. I can make a (nested) query that does what I want, but I don't know how to make this into a continuous query.
SELECT mean_value
FROM
(SELECT mean(value) AS mean_value
FROM "measurement"
WHERE time<now()
GROUP BY time(1h))
WHERE mean_value>0
The query above works, but to make it a continuous query it needs an aggregator, GROUP BY clause and a duration argument in the WHERE clause:
SELECT mean(mean_value)
FROM
(SELECT mean(value) AS mean_value FROM "db-name"."autogen"."measurement"
WHERE time<now()
GROUP BY time(1h))
WHERE mean_value>0 AND time<now()
GROUP BY time(1h)
However, this query no longer returns any values. How can I make a continuous query that excludes zeros?

Resources