Search code examples
databaseneo4jcyphergraph-databases

I'm not able to change the following code into if-else condition in Neo4j


I'm new in Neo4j and trying to compare the ScoreFT[0] with ScoreFT[1] and get that value which is greater in Neo4j.I have tried once but it has not worked.I don't have idea to use order by and Limit in the last code(Return case).Please help me.

Round,Date,Team1,FT,HT,Team2
1,(Fri) 11 Aug 2017 (32),Arsenal FC,4-3,2-2,Leicester City FC
1,(Sat) 12 Aug 2017 (32),Brighton & Hove Albion FC,0-2,0-0,Manchester City FC
1,(Sat) 12 Aug 2017 (32),Chelsea FC,2-3,0-3,Burnley FC
1,(Sat) 12 Aug 2017 (32),Crystal Palace FC,0-3,0-2,Huddersfield Town AFC
1,(Sat) 12 Aug 2017 (32),Everton FC,1-0,1-0,Stoke City FC
1,(Sat) 12 Aug 2017 (32),Southampton FC,0-0,0-0,Swansea City AFC
1,(Sat) 12 Aug 2017 (32),Watford FC,3-3,2-1,Liverpool FC
1,(Sat) 12 Aug 2017 (32),West Bromwich Albion FC,1-0,1-0,AFC Bournemouth
1,(Sun) 13 Aug 2017 (32),Manchester United FC,4-0,1-0,West Ham United FC
1,(Sun) 13 Aug 2017 (32),Newcastle United FC,0-2,0-0,Tottenham Hotspur FC

Import query

Attempt with if/else


Solution

  • CASE will only work with a single value, but you're trying to use multiple values here based upon the comparison, and worse you're trying to get a single CASE to output multiple variables, which won't work.

    It would be better to first get the values you want in a composite structure (either in 2-element lists or a map), and then use two CASEs to get the right value for each variable. Something like this, replacing your RETURN:

    ...
    WITH [t1.key, ScoreFT[0]] as t1Score, [t2.key, ScoreFT[1]] as t2Score, ScoreFT[0] > ScoreFT[1] as t1Won
    RETURN CASE WHEN t1Won THEN t1Score ELSE t2Score END as s, 
           CASE WHEN t1Won THEN t2Score ELSE t1Score END as p
    

    If you want a map instead, you can create it explicitly instead of using the lists:

    WITH t1 {.key, score:ScoreFT[0]} as t1Score, t2 {.key, score:ScoreFT[1]} as t2Score, ...