In ElasticSearch using aggs you can easily replicate this sql query
SELECT COUNT(id) FROM table;
But I want this:
SELECT COUNT(id), id FROM table;
Or even better I want to do:
SELECT COUNT(price) * price FROM table;
Is this even possible in Elasticsearch?
I have tried “terms” and similar , using pipelines or bucket_script BUT the problem is doc_count is not accessible in buckets_path .
Related
Let me explain the question.
I have two tables, which have 3 columns with same data tpyes. The 3 columns create a key/ID if you like, but the name of the columns are different in the tables.
Now I am creating queries with these 3 columns for both tables. I've managed to independently get these results
For example:
SELECT ID, FirstColumn, sum(SecondColumn)
FROM (SELECT ABC||DEF||GHI AS ID, FirstTable.*
FROM FirstTable
WHERE ThirdColumn = *1st condition*)
GROUP BY ID, FirstColumn
;
SELECT ID, SomeColumn, sum(AnotherColumn)
FROM (SELECT JKM||OPQ||RST AS ID, SecondTable.*
FROM SecondTable
WHERE AlsoSomeColumn = *2nd condition*)
GROUP BY ID, SomeColumn
;
So I make a very similar queries for two different tables. I know the results have a certain number of same rows with the ID attribute, the one I've just created in the queries. I need to check which rows in the result are not in the other query's result and vice versa.
Do I have to make temporary tables or views from the queries? Maybe join the two tables in a specific way and only run one query on them?
As a beginner I don't have any experience how to use results as an input for the next query. I'm interested what is the cleanest, most elegant way to do this.
No, you most probably don't need any "temporary" tables. WITH factoring clause would help.
Here's an example:
with
first_query as
(select id, first_column, ...
from (select ABC||DEF||GHI as id, ...)
),
second_query as
(select id, some_column, ...
from (select JKM||OPQ||RST as id, ...)
)
select id from first_query
minus
select id from second_query;
For another result you'd just switch the tables, e.g.
with ... <the same as above>
select id from second_query
minus
select id from first_query
I begin to work with the Canvas section in Kibana - and to retrieve data, it uses Elasticsearch SQL.
What I try to do is to retrieve the count of several values ; and I need to group certain values together - the ones that start with the same letters.
My SQL query looks like this :
SELECT
(SELECT COUNT(*) FROM logs WHERE status LIKE 'missingValue%'),
(SELECT COUNT(*) FROM logs WHERE status LIKE 'errorValue%'),
(SELECT COUNT(*) FROM logs WHERE status='exactErrorValue'),
(SELECT COUNT(*) FROM logs WHERE status='anotherExactErrorValue')
When I test this query, using SQL and a little database, it works
Now, I want to make this work inside an element of my canvas. I choose a horizontal bar chart to represent it.
This is my elasticsearch SQL query :
SELECT
(SELECT COUNT(*) FROM "monitoring-func-*"
WHERE status LIKE 'missingValue%'),
(SELECT COUNT(*) FROM "monitoring-func-*"
WHERE status LIKE 'errorValue%'),
(SELECT COUNT(*) FROM "monitoring-func-*"
WHERE status='exactErrorValue'),
(SELECT COUNT(*) FROM "monitoring-func-*"
WHERE status='anotherExactErrorValue')
And I get this error :
{
"error": {
"message": "[essql] > Unexpected error from Elasticsearch: [unresolved_exception] Invalid call to nullable on an unresolved object ScalarSubquery[With[{}]
\\_Project[[?COUNT(?*)]]
\\_Filter[(status) REGEX (LikePattern)#5139]
\\_UnresolvedRelation[[][index=monitoring-func-*],null,Unknown index [monitoring-func-*]],5142] AS ?"
}
}
Seeing "unknown Index", I first thought that the wildcard was the problem.
But it's not, it's perfectly fine in my others Elasticsearch queries.
Is there something about the Subqueries, the multiple SELECT, that Elasticsearch SQL doesn't handle well ?
I didn't find any ressource or topics on this, but maybe I've searched the wrong way.
Depending on your Elasticsearch version, essql either doesn't support subqueries or it is very limited, here is the documentation.
I have a multi fields table with a Countries column. I like the results from a query to be ordered by a particular country first and the rest alphabetically. In MySQL I would do something like:
Select * from myTable Order By Field(Countries,'Italy'),Countries
In Visual-FoxPro I have tried indexing the Cursor created by this query:
Select * from myTable Order By Countries
Index on Countries<>'Italy' TAG test
This would display all results for 'Italy' first, but leave the rest in an unpredictable order.
How to achieve this in Visual-FoxPro?
In VFP you can do it with something like this:
SELECT * FROM myTable WHERE Countries='Italy' ;
UNION ALL ;
SELECT * FROM (SELECT * FROM myTable WHERE Countries<>'Italy' ORDER BY Countries) as secsel
It does order by "if countries is not Italy first then Italy", Countries. Right?
In VFP you can use IIF(). ie:
Select *, iif(Countries == 'Italy', 1, 0) as CItaly ;
from myTable :
Order By CItaly,Countries
Note: If you want to do this via an index then you can use a composite index like:
index on iif(Countries = 'Italy', '1', '0') + Countries tag myCountry
How to order after grouping in Rethinkdb like in sql:
select count(id) as cid, ... from x
group by y
order by cid desc
If I properly understood your question, the query you are looking for is (in JavaScript)
r.table("x").group("y").count().ungroup().orderBy("reduction")
In Python/Ruby, it would be
r.table("x").group("y").count().ungroup().order_by("reduction")
I have one database table called ms_message: with 6 columns (id, senderid, receiverid, content, isRead, receivedTime). I want to get the latest received message group by sender in Doctrine but I cannot run the sub query or use order by and group by as well.
The query in SQL looks like this:
SELECT * FROM (SELECT * FROM ms_message WHERE receiverId = :receiver ORDER BY receivedTime) GROUP BY senderId;
I don't think that Doctrine supports queries like SELECT * FROM (SELECT ...)