带有子图聚合的递归查询
我正在尝试使用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 $ c移动100个单位$ c>到
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