带有子图聚合的递归查询 [英] Recursive query with sub-graph aggregation

查看:108
本文介绍了带有子图聚合的递归查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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

这篇关于带有子图聚合的递归查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆