Query with difference returns no data - influxdb

I've a query that uses difference function and I can't understand why it returns no data.
The query is:
SELECT
difference(FIRST(grid_power_counter)) as grid_power_consumed
FROM homesolar.origin.main GROUP BY time(15m)
If I remove the difference function it returns data:
SELECT
FIRST(grid_power_counter) as grid_power_consumed
FROM homesolar.origin.main GROUP BY time(15m)
Also, I can get results if I add a where time > now()-24h to the select with difference function.
I really can't understand that behavior. Can someone help me?

Q: My query would only work if I add the where filter to it. Why is that so?
Quoted from influxdb's Groupby time doc:
Basic GROUP BY time() queries require an InfluxQL function in the
SELECT clause and a time range in the WHERE clause.
I suspect your first DIFFERENCE query didn't work because it was missing the mandatory WHERE filter for the Groupby time(...) function.
The Group by time() clause could be returning no rows and hence not.
This could potentially be a github issue for the influx team as I think their query parser should be complaining to you about the missing where filter for Group by time.
References:
https://docs.influxdata.com/influxdb/v1.5/query_language/data_exploration/#the-group-by-clause

Related

Influx QL Variables Integer and Variable Embedding Not working

I was trying to write a simple FluxQL Query in Grafana Dashboard that uses a variable
m1(of type constant)(which contains the name of the measurement)
I created the variable m1 in grafana dashboard variables
m1 = my-measurement
and tried to run the following queries but non of them worked and they either say expression request error or No Data)
i.e
SELECT count("fails") FROM "/^${m1:raw}$/"
SELECT count("fails") FROM "/^${m1}$/"
SELECT count("fails") FROM $m1" (expression request error)
SELECT count("fails") FROM "$m1"
SELECT count("fails") FROM "${m1}"
The only query worked was without dashboard variables
SELECT count("fails") FROM "my-measurement"
How can I use the variables to work for that query.
On the similar ground I tried to make a custom variable(myVar) for which we take integer input values from user and on that basis where clause should work, but same error occurs either no data or expression request error
What I tried was
SELECT count(*) from "my-measurement-2" WHERE ("value" > $myVar)
How should I solve these issues?Please help
You may have a problem with
1.) syntax
SELECT count("fails")
FROM "${m1:raw}"
2.) data
You may correct query syntax, but query can be very inefficient. Query execution may need a lot of time - so it's better to have timefilter, which will use selected dashboard time range (make sure you have some data in that time range)
SELECT count("fails")
FROM "${m1:raw}"
WHERE $timeFilter
3.) Grafana panel configuration
Make sure you are using suitable panel - for query above Stat panel is a good option (that query returns only single value, not timeseries, so time series panel types may have a problem with that).
Generally, use query inspector to see how are variables interpolated - there can be "magic", which is not obvious - e.g. quotes which are added around numeric variables, so then it is string filtering and not numeric filtering on the InfluxDB level.

InfluxDB mixing agregation function with non-aggregat fields/values

I have a following issue:
I need to calculate difference between consecutive points where some arbitrary ID is equal. The following:
SELECT difference(value_field) FROM mesurementName WHERE "IdField" = '10'
Works, returns difference between each consecutive point with IdField BUT IdField is lost (only time is propagated to query result). In my case time is not unique (i.e. measurement may contain many points with same timestamp, but different IdField). So I tried:
SELECT difference(value_field), IdField FROM mesurementName WHERE "IdField" = '10'
which yields:
error parsing query: mixing aggregate and non-aggregate queries is not supported!!
My next attempt was using sub-query:
SELECT IdField, diff
FROM (
SELECT
difference(flow_val) as diff
FROM
mesurementA
WHERE "IdField" = '10'
)
Which resulted in always null value in IdField.
I'd like to ask you for help or suggestion how to solve issue. By the way, we are using InfluxDB 1.3, which is not supporting JOIN anymore
If anyone would stuck as I was, then solution is following:
SELECT difference(value_field) FROM mesurementName GROUP BY "IdField"
Above somehow implicitly add "IdField" to result series and is propagated to resulting measurements with INTO clause

Order by Nearest using PostGIS, RGeo, Spatial Adapter

I'm asking this question because the answers I've found in Order by nearest - PostGIS, GeoRuby, spatial_adapter wasn't able to provide a solution. I'm trying to create a controller method that can return the n closest records to a certain record's lonlat. The record it is querying against is from the same table. This concept isn't such a big stretch if I was doing this completely in SQL. That much is clear in the linked example and below is a specific case where I obtained a result:
condos_development=#
SELECT id, name FROM condos
ORDER BY ST_Distance(condos.lonlat, ST_PointFromText('POINT(-71.06 42.45)'))
condos_development-#
LIMIT 5;
My problem is in making this work with ActiveRecord. I'm using a method that was inspired by the response by #dc10 but I'm unable to create a working query either through the RGeo methods, or direct SQL. Here's what I have so far:
def find_closest_condos(num, unit)
result = Condo.order('ST_Distance(condos.lonlat, ST_PointFromText("#{unit.lonlat.as_text)}")')
.limit(5)
end
The response from this attempt is as follows:
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error
at or near "LIMIT" 10:29:50 rails.1 LINE 1: ...lonlat,
ST_PointFromText("#{unit.lonlat.as_text)}") LIMIT $1
Would someone be able to set me on the right track on how to put this work query together so that I can make it work in Rails?
The problem is with how active record is resolving your query to SQL, also the position of the Limit clause. If you change the query to this:
Condo.order("ST_Distance(lonlat, ST_GeomFromText('#{unit.lonlat.as_text}', 4326))")
.limit(num)
You should find this works.

Multiple count in single statement (esper epl)

I am trying to create an epl statement using esper for monitoring response times, something like this:
SELECT QUEUENAME, count(latency>1000) AS NUMBER_OF_SLA_BREACHES, COUNT(latency) AS TOTALS FROM ResponseWindow GROUP BY QUEUENAME
.. however the two count() gives same results, which is incorrect.
Thanks for any help correcting this query!
You need to add the filter as a second parameter to the count aggregation function like this:
SELECT QUEUENAME, count(*,latency>1000) AS NUMBER_OF_SLA_BREACHES, COUNT(latency) AS TOTALS FROM ResponseWindow GROUP BY QUEUENAME

Google Fusion Table SQL query where clause - only AND works, not OR?

My SQL query is:
SELECT * FROM 1910640 WHERE stype='P' OR stype='ERC' OR stype='PERC' ORDER BY ST_DISTANCE(geometry, LATLNG(-0.12623619999999391,51.5001524)) LIMIT 6
This results in a "parseerror". If I replace OR with AND the query returns success:
SELECT * FROM 1910640 WHERE stype='P' AND stype='ERC' AND stype='PERC' ORDER BY ST_DISTANCE(geometry, LATLNG(-0.12623619999999391,51.5001524)) LIMIT 6
Anyone else ran into this with Fusion Tables and have a solution/workaround?
The API doc does imply only AND is allowed, which came as a big surprise to me.
http://code.google.com/apis/fusiontables/docs/developers_guide.html#Querying
OR isn't supported - look at the API ref filter_conditions
IN is supported - so you can IN all your OR conditions for SType
Excuse me if I'm saying foolery, since I never have use fusion-tables, but OR queries can be split into 2 queries, one for each condition.
The main problem in this workaround is that you must implement the ORDER in programming language when merging both queries.
You can use IN instead of OR because OR is not supported by fusion tables.
http://www.w3schools.com/sql/sql_in.asp

Resources