我的用户节点中有年龄字段我需要按照以下群组按年龄分组。
'0-9',
'10-19',
'20-29',
'30-39',
'40-49',
'50-59',
'60-69',
'70-79',
'80-89',
'90-99',
'100 +'
我使用了以下查询
MATCH (n:users) RETURN count(n.age<25) as group1,count(n.age>25 AND n.age<30) as group2
但是获取错误的用户数。任何人都有想法请在这里分享。
答案 0 :(得分:4)
问题是count
函数计算变量的出现次数,无论其值如何:
UNWIND [true, false, true, true] as test
RETURN count(test = true)
所以,你可以尝试这样的事情:
UNWIND [15, 15, 26, 29, 17, 18, 7, 14, 25, 14] as age
RETURN sum(CASE WHEN age<=25 and age>10 THEN 1 ELSE 0 END) as group1,
sum(CASE WHEN age<=30 and age>25 THEN 1 ELSE 0 END) as group2
<强> UPD 强>:
在我看来,这是一种更简单的计算分布的方法:
UNWIND [15, 15, 26, 29, 17, 18, 0, 14, 25, 14] as age
WITH age,
REDUCE(acc='',
r in [ [0,10,'group1'], [10,20,'group2'], [20,30,'group3'] ] |
acc + CASE WHEN age>=r[0] AND age<r[1] THEN r[2] ELSE '' END
) as group
WHERE size(group)>0
RETURN group, count(age)