cancel
Showing results for 
Search instead for 
Did you mean: 

If statement in the for loop

jaini_kiran
Node Clone

I am new to neo4j.

MATCH (s:Bus{bus_no:14}), (l:Bus{bus_no:12})
CALL algo.shortestPath.stream(s, l, "length")
YIELD nodeId,cost
with collect(nodeId) as q
MATCH p= (a)-[r:TRANSMISSION_LINE]-(b)
where id(a)in q and id(b) in q
with  distinct(r) as rls
return collect(rls) as e

This query returns a list of relationships.
Every relationship has a property called available_cap.
l is a node having a property called load

i want to do
for every rls in e
if rls.availale_cap > l.load then print( 'ok you can connect')
else print ('you should not connect')

Please help me.

4 REPLIES 4

julie_fisher
Node Clone

You want a case statement. So the if statement turns into something like this:

MATCH (s:Bus{bus_no:14}), (l:Bus{bus_no:12})
CALL algo.shortestPath.stream(s, l, "length")
YIELD nodeId,cost
with collect(nodeId) as q
MATCH p= (a)-[r:TRANSMISSION_LINE]-(b)
where id(a)in q and id(b) in q
with  distinct(r) as rls
RETURN
CASE
WHEN rls.availale_cap > l.load THEN 'okay you can connect'
ELSE 'you should not connect' END AS status

I'm not sure exactly how you want to use this or how you want the results formatted, but the above should return one row per rls with a single 'status' column which has one of the two print statements.

For more information on case statements, you can check out the Expressions section of the Cypher manual and go to 2.3.3 CASE expressions.

Thanks for the reply.

You're welcome. Hope it helped.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.