Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j Cypher : nested case statement

Tags:

neo4j

cypher

Is there any method for nested case statements or Decode or Map in cypher ?

Case when  object1 = 'Animal'
       then case when object2 = 'CAT' then 1 else 0
                 when object2 = 'RAT' then 2 else 0
       else -9 end 
 end

There is not Syntax issue, but then the Value from inner case is not passed on to outer case. Tried even alias a variable. I have even tried the following:

case object1 = 'Animal'
      when object2 = 'CAT' then 1 
      when object2 = 'RAT' then 2
 end

This even don't work either. As "case object1 = 'Animal'" is not treated as IF condition....guess only in the WHEN.

like image 548
neoman1 Avatar asked Oct 04 '17 06:10

neoman1


1 Answers

If use nested cases:

UNWIND ['Human', 'Animal'] as var1
UNWIND ['CAT', 'RAT'] as var2
RETURN var1, var2,
       CASE WHEN var1 = 'Animal'
              THEN CASE WHEN var2 = 'CAT'
                          THEN 1
                        ELSE CASE WHEN var2 = 'RAT'
                                    THEN 2
                                  ELSE 0
                             END
                   END
            ELSE -9
       END as result

Or you can use map:

WITH  
     { Human: {
         __default: 101
       }, 
       Animal: {
         CAT: 1,
         RAT: 2,
         __default: 0
       },
       __default: -9
     } as tree
 UNWIND ['Human', 'Animal', 'Object'] as var1
 UNWIND ['RAT', 'CAT', 'DOG'] as var2
 RETURN var1, var2,
        CASE WHEN tree[var1] IS NULL THEN tree.__default 
             ELSE CASE WHEN tree[var1][var2] IS NULL THEN tree[var1].__default 
                       ELSE tree[var1][var2]
                  END
        END as result
like image 130
stdob-- Avatar answered Oct 21 '22 11:10

stdob--