I am trying to figure out how to aggregate the result of Union in Cypher. The following example can be executed in Cypher snippets live:
MATCH (n:Crew)-[r:KNOWS]->m
WHERE n.name='Neo'
RETURN n AS name,m
UNION
MATCH (n:Crew)-[r:KNOWS]->m
WHERE n.name='Morpheus'
RETURN n AS name,m
This query shows three rows as result because Neo knows one person and Morpheus two (note the directional link in the query). Let's say that we want to aggregate the known people. We can do something like this:
MATCH (n:Crew)-[r:KNOWS]->m
WHERE n.name='Neo'
RETURN n AS name,count(m) AS c
UNION
MATCH (n:Crew)-[r:KNOWS]->m
WHERE n.name='Morpheus'
RETURN n AS name,count(m) AS c
So far we are Ok. However, I don't know how to solve the problem if what we want to aggregate (the hidden Group By) is in both the first and second query. Since this doesn't happen in the previous case, let's assume for the sake of the explanation that we have the following queries:
MATCH (n:Label1)-[:label3]->(:label4)
RETURN n.name as name, n.age as value
and
MATCH (m:Label2)-[:label5]->(:label6)
RETURN m.surname as name, m.k as value
which return
John, 12
Sam, 17
and
John, 78
Tim, 12
Is it possible to do something like
(
MATCH (n:Label1)-[:label3]->(:label4)
RETURN n.name as name, n.age as value
UNION ALL
MATCH (m:Label2)-[:label5]->(:label6)
RETURN m.surname as name, m.k as value
)
RETURN name, sum(value)
to obtain the result below?
John, 90
Sam, 17
Tim, 12
Obviously, I already tried such a query and it doesn't compile. Hence, I am wondering if there is something similar.