使用 sub-graph 聚合的递归查询
Recursive query with sub-graph aggregation
我正在尝试使用 Neo4j 编写一个查询来聚合特定 sub-graph 的数量。
我们有两家商店 Store1
和 Store2
,一家与供应商 S1
合作,另一家与供应商 S2
合作。我们将 100 个单位从 Store1
移动到 Store3
,将 200 个单位从 Store2
移动到 Store3
。
然后我们将 100 个单位从 Store3
移动到 Store4
。所以现在 Store4
有 100 个单位,大约 33 个来自供应商 S1
,66 个来自供应商 S2
。
我需要查询才能有效地return此信息,例如
S1, 33
S2, 66
我有一个递归查询来汇总每条路径上的所有移动
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
Returns:
| store1.Supplier |减少|
|-------------------- |------------|
| S1 | 200 |
| S2 | 300 |
|空 | 100 |
期望:
| store1.Supplier |减少|
|--------------------|------------|
| S1 | 33.33 |
| S2 | 66.67 |
所以下面的内容很丑陋,但它适用于你给出的例子。
MATCH (s4:Store { Name:'Store4' })<-[r1:MOVE_TO]-(s3:Store)<-[r2:MOVE_TO*]-(s:Store)
WITH s3, r1.Quantity as Factor, SUM(REDUCE(amount = 0, r IN r2 | amount + r.Quantity)) AS Total
MATCH (s3)<-[r1:MOVE_TO*]-(s:Store)
WITH s.Supplier as Supplier, REDUCE(amount = 0, r IN r1 | amount + r.Quantity) AS Quantity, Factor, Total
RETURN Supplier, Quantity, Total, toFloat(Quantity) / toFloat(Total) * Factor as Proportion
我相信它可以改进。
这个怎么样:
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
哪个 returns :
supplier pct
Store1 33
Store2 67
Returned 2 rows in 151 ms
我正在尝试使用 Neo4j 编写一个查询来聚合特定 sub-graph 的数量。
我们有两家商店 Store1
和 Store2
,一家与供应商 S1
合作,另一家与供应商 S2
合作。我们将 100 个单位从 Store1
移动到 Store3
,将 200 个单位从 Store2
移动到 Store3
。
然后我们将 100 个单位从 Store3
移动到 Store4
。所以现在 Store4
有 100 个单位,大约 33 个来自供应商 S1
,66 个来自供应商 S2
。
我需要查询才能有效地return此信息,例如
S1, 33
S2, 66
我有一个递归查询来汇总每条路径上的所有移动
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
Returns:
| store1.Supplier |减少|
|-------------------- |------------|
| S1 | 200 |
| S2 | 300 |
|空 | 100 |
期望:
| store1.Supplier |减少|
|--------------------|------------|
| S1 | 33.33 |
| S2 | 66.67 |
所以下面的内容很丑陋,但它适用于你给出的例子。
MATCH (s4:Store { Name:'Store4' })<-[r1:MOVE_TO]-(s3:Store)<-[r2:MOVE_TO*]-(s:Store)
WITH s3, r1.Quantity as Factor, SUM(REDUCE(amount = 0, r IN r2 | amount + r.Quantity)) AS Total
MATCH (s3)<-[r1:MOVE_TO*]-(s:Store)
WITH s.Supplier as Supplier, REDUCE(amount = 0, r IN r1 | amount + r.Quantity) AS Quantity, Factor, Total
RETURN Supplier, Quantity, Total, toFloat(Quantity) / toFloat(Total) * Factor as Proportion
我相信它可以改进。
这个怎么样:
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
哪个 returns :
supplier pct
Store1 33
Store2 67
Returned 2 rows in 151 ms