cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Multiple Relationship - How To retrieve the maxId ? :thinking:

Hello,

I 'd like to solve this

Assume I have something like this. ( please note that Open Doors or Close Doors is only an example ) 2 relatiionship and that my goal is to retrieve the maxId between this 2.

match (n)-[r:CLOSE_DOORS]-(m)
return max(r.ID) as maxId
UNION
match (n)-[r:OPEN_DOORS]-(m)
return max(r.ID) as maxId

But the problem is that if I use two different names for the maxId it will not work,
in case the first As is - maxIdClose and the second maxIdOpen it will throws

All sub queries in an UNION must have the same column names (line 3, column 1 (offset: 68))
"UNION"

I should filter them cause then I have to retrieve back only the maxId between this two
and also I am searching for something that is performance infact
please note that I don't want to use the other Cypher syntax cause is slower than the one I wrote befoure.
talking about this one - :|CLOSE_DOORS:|OPEN_DOORS

Thank you as always for your time.

3 REPLIES 3

Hello @Andrea-Cavallo

With this trick, you can find out what type it is:

MATCH (n)-[r:CLOSE_DOORS]-(m)
RETURN "CLOSE_DOORS" AS relationship_type, max(r.ID) as maxId
UNION
MATCH (n)-[r:OPEN_DOORS]-(m)
RETURN "OPEN_DOORS" AS relationship_type, max(r.ID) as maxId

Regards,
Cobra

thank you but is like what I have wrote , what I need to do , is to retrieve back the maxId
assuming CLOSE_DOORS has maxId = 10
and OPEN_DOORS has maxId = 5
than I need to return ONLY - 10

MATCH (n)
CALL {
  WITH n
  MATCH (n)-[r:CLOSE_DOORS]-(m)
  RETURN max(r.ID) AS maxId
UNION
  WITH n
  MATCH (n)-[r:OPEN_DOORS]-(m)
  RETURN max(r.ID) AS maxId
}
RETURN maxId ORDER BY maxId DESC LIMIT 1

OR

RETURN max(maxId) AS maxId