在CASE中使用子查询是否正确?
MATCH (p:Person)
SET p.total =
CASE
WHEN HAS (p.total)
THEN p.total
ELSE
MATCH (p)-[:CHILD]->(c)
RETURN sum(c.age)
END
RETURN DISTINCT p
当我尝试这样做时,我收到以下错误。
无效输入']':预期标识符字符,空格,NodeLabel,属性映射或关系模式 “匹配(p) - [:儿童] - >(c)”
答案 0 :(得分:1)
你可以做到,但将来会有更好的方法:
MATCH (p:Person)
SET p.total =
CASE WHEN HAS (p.total)
THEN p.total
ELSE reduce(sum = 0, path in (p)-[:CHILD]->() | sum + (nodes(path)[1]).age)
END
RETURN DISTINCT p
或实际上:
MATCH (p:Person)
SET p.total =
COALSECE(p.total,
reduce(sum = 0, path in (p)-[:CHILD]->() | sum + (nodes(path)[1]).age)
)
RETURN DISTINCT p