2014-10-11 68 views
2

我想執行這個查詢暗號嵌套收集查詢不正確地聚集

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] |   | 

我希望這是清楚我所要求的。

+0

不要使用不同的聚合它沒有意義 – 2014-10-11 22:38:26

+0

謝謝,我知道,但與此查詢,如果我不使用DISTINCT它計數所有的結果和perfoms假計數 – 2014-10-12 09:09:32

回答

1
  1. 不要使用與聚合不同
  2. 不聚集您返回簡單值(這是你的主要問題)相同的值
  3. 不匹配兩次

這裏是一個應該更好的變體,你沒有分享你的樣品圖,所以我不能嘗試它

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 
+0

你是對的,對不起,如果我沒有立即捕捉到,這絕對是我的問題的解決方案。非常感謝你 – 2014-10-12 12:44:15