how to have two aggregation in cypher query in neo

2019-06-08 21:08发布

问题:

i have problem with complex queries; here is my cypher query:

params.put("query", "name:*");
ExecutionResult result = engine.execute( "start n=node:groups({query}) 
match n<-[:Members_In]-x 
with n,count(distinct x) as numberOfUsers
where numOfUsers>avg(numOfUsers) 
return  n.name,numOfUsers ", params );

n is the group name and x is the users of each group. how can i have avg of group users count and compare it with each group users count? can I get the average number of group users and then return the groups with more users than avg? Thanks.

回答1:

I don't think you can get the avg and the counts in the same query, without restarting. So here's my attempt:

start n=node:groups({query}) 
match n<-[:Members_In]-x 
with n, count(distinct x) as cnt
with avg(cnt) as av
start n=node:groups({query}) 
match n<-[:Members_In]-x
with n, av, count(distinct x) as numOfUsers
where numOfUsers > av
return n.name, numOfUsers, av


标签: neo4j cypher