Cypher嵌套收集查询未正确聚合

时间:2014-10-11 17:26:50

标签: neo4j cypher collect

我想执行此查询

MATCH (p:Person)-->(s:Startup)
WITH DISTINCT p
MATCH (p)-->(s:Startup)-->(m:Market)
WITH DISTINCT p, s, m, COLLECT(m) as markets
ORDER BY id(s)
RETURN DISTINCT p, COUNT(DISTINCT s) as inv, 
                COLLECT({id: id(s), markets: markets}) as startups, 
                count (m) as mctotal
ORDER BY inv DESC
LIMIT 10

但我无法弄清楚为什么它没有正确地与他们的市场聚合创业公司。 这是我查询的结果:

+-----+-----+------------+---------+
|  p  | inv | startups   | mctotal |
+-----+-----+------------+---------+
|  p  |  11 | id: 1      |    7    |
|     |     | markets: 1 |         |
|     |     |            |         |
|     |     | id: 1      |         |
|     |     | markets: 2 |         |
|     |     |            |         |
|     |     | id: 2      |         |
|     |     | markets: 3 |         |
|     |     |            |         |
|     |     | id: 2      |         |
|     |     | markets: 4 |         |

......等等。

我想获得的结果如下:

+-----+-----+---------------+---------+
|  p  | inv | startups      | mctotal |
+-----+-----+---------------+---------+
|  p  |  11 | id: 1         |    7    |
|     |     | markets:[1,2] |         |
|     |     |               |         |
|     |     | id: 2         |         |
|     |     | markets:[3,4] |         |

我希望我清楚要求的是什么。

1 个答案:

答案 0 :(得分:1)

  1. 不要使用distinct with aggregation
  2. 不会将您返回的相同值汇总为简单值(这是您的主要问题)
  3. 两次不匹配
  4. 这是一个应该更好的变体,你没有分享你的样本图,所以我无法尝试

     MATCH (p)-->(s:Startup)-->(m:Market)
     WITH p, s, COLLECT(m) as markets, count(m) as mcount
     ORDER BY id(s)
     RETURN p, COUNT(DISTINCT s) as inv, 
               COLLECT({id: id(s), markets: markets}) as startups, 
               sum(mcount) as mctotal
     ORDER BY inv DESC
     LIMIT 10