Neo4j Cypher:嵌套的case语句

时间:2017-10-04 06:15:33

标签: neo4j cypher

嵌套的case语句或cypher中的Decode或Map是否有任何方法?

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

没有语法问题,但是内部的值不会传递给外壳。甚至试了一个变量别名。 我甚至尝试过以下方法:

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

这甚至都不起作用。作为" case object1 =' Animal'"不被视为IF条件....只在WHEN中猜测。

1 个答案:

答案 0 :(得分:3)

如果使用嵌套案例:

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

或者你可以使用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