Run inner join query in Grafana where datasource is influx - grafana

I need to run inner join query in Grafana where datasource is InfluxDB.
In sql query will be:
SELECT t.status,count(t.taskName) FROM TasksStatus t
INNER JOIN TasksStatus s
ON t.taskName = s.taskName
WHERE t.modified > s.modified
GROUP BY t.status;
Getting Error while running in grafana
InfluxDB Error: error parsing query: found t, expected ; at line 1, char 71

https://docs.influxdata.com/influxdb/v1.8/query_language/
InfluxQL, the InfluxDB SQL-like query language
It is only SQL-like, but it not a SQL language. InfluxQL doc doesn't mention any support for INNER JOIN, so it is not suported by InfluxQL.

Related

"Spectrum nested query error" Redshift error

When I run this query in Redshift:
select sd.device_id
from devices.s_devices sd
left join devices.c_devices cd
on sd.device_id = cd.device_id
I get an error like this:
ERROR: Spectrum nested query error
DETAIL:
-----------------------------------------------
error: Spectrum nested query error
code: 8001
context: A subquery that refers to a nested table cannot refer to any other table.
query: 0
location: nested_query_rewriter.cpp:726
process: padbmaster [pid=6361]
-----------------------------------------------
I'm not too sure what this error means. I'm only joining to one table I'm not sure which "other table" it's referring to, and I can't find much info about this error on the web.
I've noticed if I change it from left join to join, the error goes away, but I do need to do a left join.
Any ideas what I'm doing wrong?
Redshift reference mentions:
If a FROM clause in a subquery refers to a nested table, it can't refer to any other table.
In your example, you're trying to join two nested columns in one statement.
I would try to first unnest them separately and only then join:
with
s_dev as (select sd.device_id from devices.s_devices sd),
c_dev as (select cd.device_id from devices.c_devices cd)
select
c_dev.device_id
from c_dev
left join s_dev
on s_dev.device_id = c_dev.device_id
The solution that worked for me, was to create a temporary table with the nested table's data and then join the temp table with the rest of the tables I needed to.
For example, if the nested table is spectrum.customers, the solution will be:
DROP TABLE IF EXISTS temp_spectrum_customers;
CREATE TEMPORARY TABLE
temp_spectrum_customers AS
SELECT c.id, o.shipdate, c.customer_id
FROM spectrum.customers c,
c.orders o;
SELECT tc.id, tc.shipdate, tc.customer_id, d.delivery_carrier
FROM temp_spectrum_customers tc
LEFT OUTER JOIN orders_delivery d on tc.id = d.order_id;

Problem with a query using GROUP BY in PostgreSQL

I'm using the query tool in PGAdmin 4.20 and trying the following query:
select * from metadatavalue group by resource_id order by resource_id;
And I'm getting the following:
ERROR: column "metadatavalue.metadata_value_id" must appear in the
GROUP BY clause or be used in an aggregate function LINE 3: select *
from metadatavalue group by resource_id order by re...
^ SQL state: 42803 Character: 176
The thing is that in another table, I use the same syntax and works:
select * from metadatafieldregistry group by metadata_field_id order by metadata_field_id;
Also, I'm not getting all the entries from a same resource_id, only a few. Could these two problems be related?
Please, help!
Thank you in advance.

Redshift pg_user table throws Invalid Operation error on JOIN

when I run the following query,
select * from stl_query q
join pg_user u on q.userid = u.usesysid
where u.usename = 'admin';
I get the following error:
SQL Error [500310] [0A000]: [Amazon](500310) Invalid operation: Specified types or functions (one per INFO message) not supported on Redshift tables.;
The query is run on the leader node. What am I doing wrong?
pg_user is a Leader Node-Only Function and cannot be mixed with functions that are not Leader Node-Only.
From the documentation:
Some Amazon Redshift SQL functions are supported only on the leader
node and are not supported on the compute nodes. A query that uses a
leader-node function must execute exclusively on the leader node, not
on the compute nodes, or it will return an error.
The documentation for each leader-node only function includes a note
stating that the function will return an error if it references
user-defined tables or Amazon Redshift system tables.
Source: http://docs.aws.amazon.com/redshift/latest/dg/c_SQL_functions_leader_node_only.html
As a work around in your scenario, you can generate a temp table with the subset of pg_user data that you need, then join to that temp table.
select usesysid
into temp table tmp_user
from pg_user
where usename = 'admin';
select * from stl_query q
inner join tmp_user u on q.userid = u.usesysid;

JPQL #Query causing "unexpected token: ("

I have following method defined:
#Query("SELECT AVG(total) FROM (SELECT COUNT(t.name) total FROM DataTable t GROUP BY DATE(actiontime)) result")
Long countAvg();
However it causes this error:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 24 [SELECT AVG(total) FROM (SELECT COUNT(t.name) total FROM backend.DataTable t GROUP BY DATE(actiontime)) result]
But following SQL works fine:
SELECT AVG(total) FROM (SELECT COUNT(NAME) total FROM DATA_TABLE GROUP BY DATE(actiontime)) result
If i understand correctly, JPQL has problems with subquery. How should i create this kind of query then?
I don't think jpql supports subselect with from clause. As per jpa docs
Subqueries are restricted to the WHERE and HAVING clauses in this release. Support for subqueries in the FROM clause will be considered in a later release of the specification.
You can use nativeQuery = true in the #Query annotation and run it as native query instead or rewrite the query if possible.

IBM db2 union query bad results

When I execute this query in SQL Server which calls to IBM,
Select * from openquery(ibm,'
Select COST_AMT,'Query1' as Query
from table
where clause
with ur;
')
union
Select * from openquery(ibm,'
Select COST_AMT,'Query2' as Query
from table
different where clause
with ur;
')
I get different results in the union query than when I execute them separately and bring the results in together. I have tried the union query inside the openquery so I believe this is an IBM thing. The results appear to be a distinct selection of COST_AMT sorted by lowest to highest.
ie:
1,Query1
2,Query1
3,Query1
1,Query2
2,Query2
3,Query2
but the data is actually like this:
1,Query1
1,Query1
1,Query1
2,Query1
2,Query1
3,Query1
1,Query2
1,Query2
1,Query2
2,Query2
2,Query2
3,Query1
Am I missing something about the ibm union query? I realize I could sum and get the answer, (which is what I plan no doing) but I want to know more about why this is happening.
This has nothing to do with "ibm" or "db2" -- the SQL UNION operator removes duplicates. To retain duplicates use UNION ALL.