Combine 2 queries with differnt column names

I m trying to union/ combine 2 query results with different relationship types that should stay separate afterwards (sub queries r2, r3 not the same column names). I want to get an output graph with c, p, r2, r3.
I tried the following cypher query:

MATCH (p:Sample)-[r3:`Age`]->(c:Sample)
WHERE
r3.age >= 50
RETURN p, r3, c 

UNION 

MATCH (p:Sample)-[r1:`Age`]->(c:Sample)
MATCH (p:Sample)-[r2:`Size`]->(c:Sample)
WHERE
(r2.size <= 50 and r1.age >= 20)
return p, r2, c

The error message is of course:

Neo.ClientError.Statement.SyntaxError: All sub queries in an UNION must have the same column names (line 5, column 1 (offset: 98))
"UNION "

The result should look like this table:

    p |   r2  | r3   | c 
___________________________________
   p1 |       | sth  | c1
   p1 |  sth  |      | c1 
   ...|  ...  |  ... | ...

try this

MATCH (p:Sample)-[r3:`Age`]->(c:Sample)
WHERE
r3.age >= 50
RETURN p, null as r2, r3, c 

UNION 

MATCH (p:Sample)-[r1:`Age`]->(c:Sample)
MATCH (p:Sample)-[r2:`Size`]->(c:Sample)
WHERE
(r2.size <= 50 and r1.age >= 20)
return p, r2, null as r3, c

You basically return null for the missing columns - now both queries have the same return columns

1 Like