How can I count distinct elements with Memgraph? - memgraphdb

I was wondering if Memgraph supports something like COUNT(DISTINCT x) and if not how can I achieve what I'm looking for?

The aggregation function COUNT(DISTINCT x) can be rewritten by using WITH DISTINCT variable RETURN count(x).

Related

Filter Array in Hive

An Apache hive table has the following column definition:
myvars:array<struct<index:bigint,value:string>>
An example for the corresponding data is:
"myvars":[
{"index":2,"value":"value1"}
, {"index":1,"value":"value2"}
, {"index":2,"value":"value3"}
]
How can this array be filtered to all elements where "index"==2.
In JavaScript I would do something like the following:
myvars.filter(function(d){return d.index==2;})
How can the same result be achieved with Apache Hive QL, preferably without lateral views?
In hive you have a set of Collection functions:
Collection
array_contains(Array<T> a, val)
array<K.V> map_keys(Map<K.V> a)
array<K.V> map_values(Map<K.V> a)
size(Map<K.V>|Array<T> a)
sort_array(Array<T> a)
in your query use
...
WHERE
array_contains(myvars,2)
I think if you are trying to extract all the values where index is 2, you want something like this:
SELECT DISTINCT value
FROM mytable
LATERAL VIEW EXPLODE(myvars) exploded_myvars AS idx, value
WHERE idx = 2;
If instead the data type was array<map<string,string>> it would be
SELECT DISTINCT mv["value"]
FROM mytable
LATERAL VIEW EXPLODE(myvars) exploded_myvars AS mv
WHERE mv["index"] = 2;

order by after grouping in RethinkDB

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")

Does oracle /netezza execute subqueries only once

In this query:
select L1.*, L2.* from (select t1.col1, max(t1.col2) from ($subquery) t1, group by t1.col1) L1, ($subquery) L2 where L1.col1 = L2.col1
Will Oracle/Netezza execute the $subquery only once? Or twice?
Oracle will execute two times. (Actually this isn't 100% accurate. It will execute only the main (compound) query once, but probably it will access the subquery's table's 2 different times/way.)
You should use with for optimizing this way.
(Otherwise you could use MAX() OVER (PARTITION BY ....) for similar things as well.)

Oracle: function based index using dynamic values

I have one complex SQL queries. One of the simple part of the queries looks like:
Query 1:
SELECT *
FROM table1 t1, table2 t2
WHERE t1.number = t2.number
AND UPPER(t1.name) = UPPER(t2.name)
AND t1.prefix = p_in_prefix;
Query 2:
SELECT *
FROM table1 t1, table2 t2
WHERE t1.number = t2.number
AND UPPER(t1.name) = UPPER(p_in_prefix || t2.name)
AND t1.prefix = p_in_prefix;
I have function based index on table1 as (number, UPPER(name)). I have function based index on my table2 as (number, UPPER(NAME)). p_in_prefix is a input parameter (basically a number).
Because of these indexes my Query 1 runs efficiently. But Query 2 has a performance issue, as in Query 2, 't2.name' is prefixed with p_in_prefix.
I can not create function based index for Query 2 because p_in_prefix is a input parameter and I don't know while creating index, what values it might hold. How to resolve performace issue in this scenario? Any hint/idea would be appreciated. If you require more information, please let me know.
Thanks.
Use AND UPPER(t1.name) = UPPER(p_in_prefix) || UPPER(t2.name).
As you have a function based index as UPPER(NAME) of table2, you should have an operand with the same expression in the query in order to make use of the function based index.
Using UPPER(p_in_prefix || t2.name) will not use the function based index as this does not match the function expression UPPER(NAME). Note here that using UPPER(t2.name) does not cause any problems as t2 is just a column alias.
Along with this, you can also pass an optimizer hint in your query in order to instruct the optimizer to use the index.
For more information read "Oracle Database 11g SQL" by Jason Price.
Also read Oracle Docs here and here and for optimizer hints here.

Oracle inner query

My query
select kc.prod_id, kc.prod_actv_ts
from kit_cmpnt kc ,kit_cmpnt_stock kcs, prod p
where kc.cmpnt_cd='016'
and kcs.kit_cmpnt_nbr= kc.kit_cmpnt_nbr
and kcs.stock_id=1
and kcs.prod_id=kc.prod_id
and kcs.prod_actv_ts=kc.prod_actv_ts
and p.prod_id= kc.prod_id
and p.prod_actv_ts= kc.prod_actv_ts
and p.prod_inactv_ts is null;
I want to get a distinct combination of kc.prod_id, kc.prod_actv_ts
like distinct(kc.prod_id, kc.prod_actv_ts)
But what i am getting is a combination of repeated prod_id and prod_actv_ts
please help
I'd restructure the query as follows :
select kc.prod_id, kc.prod_actv_ts
from kit_cmpnt kc
where kc.cmpnt_cd='016'
and exists
(select 1 from kit_cmpnt_stock kcs
where kcs.stock_id=1
and kcs.kit_cmpnt_nbr= kc.kit_cmpnt_nbr
and kcs.prod_id=kc.prod_id
and kcs.prod_actv_ts=kc.prod_actv_ts)
and exists
(select 1 from prod p
where p.prod_id= kc.prod_id
and p.prod_actv_ts= kc.prod_actv_ts
and p.prod_inactv_ts is null);
General principle is that you shouldn't have something in the FROM clause unless you are taking something from it. If you aren't taking anything from it, it is a filter and should be in the WHERE clause as a subquery.
Try using select * to find the reason for the duplicates.
use distinct to filter unique combination...
eg..
select distinct(a,b,c) from table
where
some condition

Resources