I'm trying to get the total rows of my union but it only shows a total of 1 for each (rather than 2)
CALL {
OPTIONAL MATCH
(a:Account {
id : $account_id
})
-[:MEMBER]->
(n:Workspace)
RETURN n
UNION
OPTIONAL MATCH
(n:Workspace {
account_id: $account_id
})
RETURN n
}
WITH n, COUNT(n) AS total
RETURN n, total
Is it possible to aggregate the total?
Current non-UNION query (works)
OPTIONAL MATCH
(a:Account {
id : $account_id
})
-[:MEMBER]->
(n:Workspace)
WITH
COLLECT(n) AS nodes,
COUNT(n) AS total
OPTIONAL MATCH
(n:Workspace {
account_id: $account_id
})
WITH
nodes + COLLECT(n) AS n,
total + COUNT(n) AS total
UNWIND n AS node
WITH
node,
total
SKIP $skip
LIMIT $limit
WITH
COLLECT(node) AS results,
total
RETURN results, total
If I understand you correctly, you get two rows with a result of 1 instead of 1 row with the result of 2. When aggregating in cypher it does implied groupby, meaning that all the columns in the WITH or RETURN statement will be used in the groupby statement by default. So if you want to return only a total count you can use:
CALL {
OPTIONAL MATCH
(a:Account {
id : $account_id
})
-[:MEMBER]->
(n:Workspace)
RETURN n
UNION
OPTIONAL MATCH
(n:Workspace {
account_id: $account_id
})
RETURN n
}
WITH n
RETURN count(*) as total
Related
With my following query, I expected a r to be an array and t to be a number. But when I run the query, it just shows (no changes, no records).
When run individually, their output is as expected. So what is wrong with my query?
MATCH
(a:Account {
id : $account_id
})
-[:MEMBER]->
(n:Workspace)
WITH
COLLECT(n) AS nodes,
COUNT(n) AS total
MATCH
(n:Workspace {
account_id: $account_id
})
WITH
nodes + COLLECT(n) AS r,
total + COUNT(n) AS t
RETURN r, t
I want to get all the list of distinct nodes and relationship that I am getting through this query.
MATCH (a:Protein{name:'9606.ENSP00000005995'})-[r:ON_INTERACTION_WITH]-(b:Protein)-[d:ON_INTERACTION_WITH]-(c:Protein)
Return a,b,c,d,r
limit 10
This should work:
MATCH (a:Protein{name:'9606.ENSP00000005995'})-[r:ON_INTERACTION_WITH]-(b:Protein)-[d:ON_INTERACTION_WITH]-(c:Protein)
WITH * LIMIT 10
RETURN
COLLECT(DISTINCT a) AS aList,
COLLECT(DISTINCT b) AS bList,
COLLECT(DISTINCT c) AS cList,
COLLECT(DISTINCT r) AS rList,
COLLECT(DISTINCT d) AS dList
match(n)-[r:LIKES]->(m) with count(n) as cnt, m where cnt = max(cnt) return m
Above query results in following error:
Invalid use of aggregating function max(...) in this context (line 1,
column 61 (offset: 60))
This query should return a collection of the m nodes that have the maximum count. It only needs to perform a single MATCH operation, and should be relatively performant.
MATCH (n)-[:LIKES]->(m)
WITH m, COUNT(n) AS cnt
WITH COLLECT({m: m, cnt: cnt}) AS data, MAX(cnt) AS maxcnt
RETURN REDUCE(ms = [], d IN data | CASE WHEN d.cnt = maxcnt THEN ms + d.m ELSE ms END) AS ms;
If you're just trying to find a single node that has the most LIKES relationships leading to it, then you can add an ORDER BY and LIMIT:
MATCH (n)-[:LIKES]->(m)
WITH m, count(n) AS cnt
ORDER BY cnt DESC
LIMIT 1
RETURN m
However, that query has the limitation in that if more than one node has the maximum number of inbound relationships, then those tied nodes won't be returned. To achieve that result, you might try something like this:
MATCH (n)-[:LIKES]->(m)
WITH m, count(n) AS cnt
WITH MAX(cnt) AS maxcnt
MATCH (o)
WHERE size((o)<-[:LIKES]-()) = maxcnt
RETURN o
I want to return the nodes which does't have a specific key.
Nodes:
A(1:"xa",2:"ya",3:"za")
B(1:"xb",2:"yb",3:"zb")
C(2:"yc",3:"zc")
Now, I want to return C node because it doesn't have a key called 1.
My Approach:
MATCH (n:Company)
with n,keys(n) as keys_set
WIth n,keys_set, max(size(keys_set)) As maxi
RETURN n where keys_set < maxi
Error:
Invalid input 'h': expected 'i/I' (line 1, column 99 (offset: 98)) "MATCH (n:Company) with n,keys(n) as keys_set WIth n,keys_set,max(size(keys_set))As maxi RETURN n where keys_set
MATCH (n:Company)
WHERE NOT EXISTS(n.`1`)
RETURN n
Given the following query
match (a)-->(b)-->(c)
where id(a) = 0
return b.name, collect(c) as cs
How it's possible to return a collection composed of only a couple of fields, instead of the whole nodes?
A single field is easy:
match (a)-->(b)-->(c)
where id(a) = 0
return b.name, collect(c.fieldName) as cs
For multiple field names, maybe concatenate them?
match (a)-->(b)-->(c)
where id(a) = 0
return b.name, collect(c.fieldName1 + delimiter + c.fieldName2) as cs
match (a)-->(b)-->(c)
where id(a) = 0
return b.name, collect( { field1: c.field1, field2: c.field2 } ) as cs