Neo4J - 卷起价值

时间:2015-06-30 20:25:02

标签: neo4j cypher

我正在尝试执行以下操作:

  1. 逐层遍历树级
  2. 检查当前级别是否存在特定属性
  3. 如果存在,请将其退回。应将此级别的所有元素的总和分配给父级的价格属性
  4. 如果没有,请转到下一级并从第2步开始。如果它达到最低级别,则从分配给房间的RoomType中获取它,将此值分配给它的price属性并汇总值。
  5. 初始图表: enter image description here

    查询后的图表: enter image description here

    假设在运行查询之前不存在price属性

    这是我提出的,但不知道如何继续。

    MATCH (b:Building) 
    SET b.price = 
       CASE WHEN has(b.price) THEN b.price
       ELSE 
         // Go to the next level, compute price for all children and assign the sum of children to b.price. 
       END
     RETURN b
    

1 个答案:

答案 0 :(得分:0)

这样的东西?

MATCH (parent:Building)
WHERE not has(parent.price)
match (parent)-->(child)
WITH parent, sum(child.price) as price
SET parent.price = price

更新

MATCH path=(root:Building)-[*0..]->(parent)-->(child)
WITH parent,child, length(path) as l
ORDER BY l desc
WITH parent, sum(child.price) as price
SET parent.price = coalesce( parent.price, price)