TheWalkingData
TheWalkingData

Reputation: 1067

How to create a relationship between two nodes with common properties?

I think I am asking this question right. The data in my node looks like this:

Car      Speed
---      -----
Farrari  80
Corvette 92
Farrari  135
Corvette 129
Porche   78
Porche   150

I want to create a relationship called HIGH_SPEED between the two same cars with the highest speed and the lowest speed of the car. So essentially relationship should look like this:

                  ___________                        ___________
                 /           \                      /           \
                /             \    High Speed      /             \
               |  Car: Porche  |_________________\|  Car: Porche  |  
               |  Speed: 78    |                 /|  Speed: 150   |
                \             /                    \             /
                 \___________/                      \___________/

This is what I have got so far. This creates an empty relationship.

MATCH (c_max:CARS), (c_min:CARS)
with c_max.name as max_name, max(c_max.speed) as max_speed
   , c_min.name as min_name, min(c_min.speed) as min_speed
WHERE c_max.name = c_min.name
FOREACH (x IN max_name |
  FOREACH (y IN min_name |
    CREATE (x)-[:HIGH_SPEED]->(y)))

Upvotes: 3

Views: 259

Answers (1)

Dave Bennett
Dave Bennett

Reputation: 11216

How about if you found the min and max for each type of car and then matched the nodes directly for the highest and the lowest speeds and connect them.

match (c:CARS)
with c.name as type, min(c.score) as min_speed, max(c.score) as max_speed
match (c1:CARS {name: type, score: min_speed}), (c2:CARS {name: type, score: max_speed})
create (c1)-[:HIGH_SPEED]->(c2)
return c1, c2

Upvotes: 3

Related Questions