带有子图聚合的递归查询

编程入门 行业动态 更新时间:2024-10-17 15:26:03
本文介绍了带有子图聚合的递归查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用Neo4j编写一个查询,该查询沿特定子图聚合数量。

I am trying to use Neo4j to write a query that aggregates quantities along a particular sub-graph.

我们有两个商店 Store1 和 Store2 供应商 S1 的另一个供应商 S2 。我们将100个单位从 Store1 移至 Store3 ,将200个单位从 Store2 移至 Store3 。

We have two stores Store1 and Store2 one with supplier S1 the other with supplier S2. We move 100 units from Store1 into Store3 and 200 units from Store2 to Store3.

然后从 Store3 到 Store4 。因此,现在 Store4 有100个单位,大约33个来自供应商 S1 ,另外66个来自供应商 S2 。

We then move 100 units from Store3 to Store4. So now Store4 has 100 units and approximately 33 originated from supplier S1 and 66 from supplier S2.

我需要查询才能有效地返回此信息,例如

I need the query to effectively return this information, E.g.

S1,33 S2,66

S1, 33 S2, 66

我有一个递归查询来汇总每个路径上的所有运动

I have a recursive query to aggregate all the movements along each path

MATCH p=(store1:Store)-[m:MOVE_TO*]->(store2:Store { Name: 'Store4'}) RETURN store1.Supplier, reduce(amount = 0, n IN relationships(p) | amount + n.Quantity) AS reduction

返回值:

| store1。供应商|减少| | -------------------- | ------------- | | S1 | 200 | | S2 | 300 | |空| 100 |

| store1.Supplier | reduction| |-------------------- |-------------| | S1 | 200 | | S2 | 300 | | null | 100 |

所需

| store1。供应商|减少| | --------------------- | ------------- | | S1 | 33.33 | | S2 | 66.67 |

| store1.Supplier | reduction| |---------------------|-------------| | S1 | 33.33 | | S2 | 66.67 |

推荐答案

这是怎么回事:

MATCH (s:Store) WHERE s.name = 'Store4' MATCH (s)<-[t:MOVE_TO]-()<-[r:MOVE_TO]-(supp) WITH t.qty as total, collect(r) as movements WITH total, movements, reduce(totalSupplier = 0, r IN movements | totalSupplier + r.qty) as supCount UNWIND movements as movement RETURN startNode(movement).name as supplier, round(100.0*movement.qty/supCount) as pct

返回哪个:

supplier pct Store1 33 Store2 67 Returned 2 rows in 151 ms

更多推荐

带有子图聚合的递归查询

本文发布于:2023-11-22 06:02:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616239.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!