How to find top 10 sum of each match?

时间:2016-02-03 02:46:59

标签: database neo4j cypher graph-databases

I mean if I have several X and several Y and I do a match like this:

X -[ W ]-> Y

With X and Y related by several W ( there can be several W between same pairs (X,Y) )

I want top ten X for each Y with the property sum(W.property)

If I return

return Y , sum(W.property) , X order by sum(W.property) desc Limit 10

I Just get 10 but I need for every Y,

Is there a way to do that?

1 个答案:

答案 0 :(得分:3)

MATCH X -[ W ]-> Y
WITH Y, sum(W.property) AS total, X
ORDER BY total DESC
WITH Y, collect({sum: total, X: X})[0..10] AS values
UNWIND values AS value
RETURN Y, value.sum, value.X

You can actually skip the UNWIND and just change that second WITH to a RETURN if you're OK with it returning it as an array. It would be a bit more efficient because you're not repeating values of Y over and over. If you were going to do that you could even change the map structure into an array like this:

collect([total, X])[0..10]