如何在Cypher支架查询语言使用类似于SQL的GROUP BY,在Neo4j的?(How to u

2019-10-17 10:52发布

我想找到的所有用户在公司的数量和它的男性和女性人数。 我的查询是:

 start n=node:company(name:"comp")
 match n<-[:Members_In]-x, n<-[:Members_In]-y  
 where x.Sex='Male' and y.Sex='Female' 
 return n.name as companyName, count(distinct x) as NumOfMale,
 count(distinct y) as NumOfFemale" );

我的查询是正确的,但我知道我不应该使用n<-[:Members_In]-y比赛子句。

我怎样才能得到男性的数量,女性的人数,以及用户的总数是多少?

Answer 1:

彼得在这里创建了一个跨产品:

我想这样的作品为您的使用情况较好

start n=node:node_auto_index(name='comp')
match n<-[:Members_In]-x
with  n.name as companyName, collect(x) as employees
return length(filter(x in employees : x.Sex='Male')) as NumOfMale,
length(filter(x in employees : x.Sex='Female')) as NumOfFemale,
length(employees) as Total

看到http://console.neo4j.org/r/msamaa



Answer 2:

尝试

start n=node:node_auto_index(name='comp')
match n<-[:Members_In]-x, n<-[:Members_In]-y  
where x.Sex='Male' and y.Sex='Female' 
with 
n.name as companyName, 
count(distinct x) as NumOfMale,
count(distinct y) as NumOfFemale
return NumOfMale, NumOfFemale, NumOfMale + NumOfFemale as Total

见http://tinyurl.com/cjpxrax的一个例子。



Answer 3:

其实还有一种更简单的方式来实现这一结果

START n=node:node_auto_index(name='comp') 
MATCH n<-[:Members_In]-x 
RETURN count(x.name), x.Sex

看看http://architects.dzone.com/articles/neo4jcypher-sql-style-group应该是相当有益的,以为我的答案是有点晚...



文章来源: How to use SQL-like GROUP BY in Cypher query language, in Neo4j?
标签: neo4j cypher