'CypherQL Sum Rows

I have a union query:

match (a)--(r)--(b) where a.name contains "Test" return count(a)
union all 
match (a)--(b) where a.name contains "Test" return count(a)

This returns something like

5
8

I would like to execute both queries and return the sum. How can i achieve this?

13


Solution 1:[1]

UNION ALL is a set operation, so it will not work for this case. Try this,

MATCH (a)--(r)--(b) where a.name contains "Test" 
WITH count(a) as c1
OPTIONAL MATCH (a)--(b) where a.name contains "Test" 
RETURN c1 + count(a)

Solution 2:[2]

Newer version of Neo4j 4.x now allows processing of UNION ALL using subqueries. You may read the document below to find out more features of it.

Ref: https://neo4j.com/developer/kb/post-union-processing/

For your example;

CALL {
match (a)--(r)--(b) where a.name contains "Test" return count(a) as cnts
union all 
match (a)--(b) where a.name contains "Test" return count(a) as cnts
}
WITH sum(cnts) as totalCnts
RETURN totalCnts

RESULT:
?????????????
?"totalCnts"?
?????????????
? 13        ?
?????????????

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aldrin
Solution 2 jose_bacoy