1

我想找出公司中所有用户的数量以及男性和女性的数量。我的查询是:

 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在 match 子句中使用。

如何获取男性人数、女性人数和总用户数?

4

3 回答 3

3

Peter 在这里创建了一个叉积:

我认为这更适合您的用例

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

于 2012-12-06T11:22:14.893 回答
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 。

于 2012-12-06T09:52:42.080 回答
0

实际上有一种更简单的方法可以实现该结果

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 应该很有帮助,认为我的回答有点晚了......

于 2013-08-20T11:18:23.470 回答