Cypher:一个集合函数在另一个之上

时间:2016-01-19 14:54:11

标签: neo4j cypher aggregate

我正在寻找一种方法来在另一个的结果之上执行一个聚合函数。特别是,我想加入以下两个问题:

MATCH (e :Event) - [:ATTENDED_BY] -> (a :Person)
WITH e, collect(a) AS attendants
WHERE ALL (a in attendants WHERE a.Company="XYZ")
RETURN e.name AS name, count(*) as number_occurrences
ORDER BY number_events DESC;

MATCH (e:Event) - [:ATTENDED_BY] -> (a :Person)
WITH e, collect(a) AS attendants
WHERE ALL (a in attendants WHERE a.Company="XYZ")
WITH e.name AS name, count(*) as number_occurrences
RETURN percentileDisc(number_occurrences,0.95) as percentile;

第一个查询提供所有事件名称,只有来自单个公司(“XYZ”)的人员才会参加,以及这些事件的发生次数。第二个返回前5%最常见事件的最小出现次数。我想得到的是这些5%最常见事件的名称和出现次数。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

当然,我设法使用WITH子句来解决查询,但关键是要正确理解它的用法。这个想法是只有最后一个WITH子句传递的变量才能进一步显示。这就是为什么在我们获得"百分位数"在查询的第一部分中,我们需要在所有后续的WITH子句中继续在查询的第二部分传递它。

MATCH (e :Event) - [:ATTENDED_BY] -> (a :Person)
WITH e, collect(a) AS attendants
WHERE ALL (a in attendants WHERE a.Company="XYZ")
WITH e.name AS name, count(*) as number_occurences
WITH percentileDisc(number_occurencies,0.95) as percentile
MATCH (e :Event) - [:ATTENDED_BY] -> (a :Person)
WITH percentile, e, collect(a) AS attendants
WHERE ALL (a in attendants WHERE a.Company="XYZ")
WITH percentile, e.name AS name, count(*) as number_occurences
WHERE number_occurences > percentile
RETURN name, number_occurences
ORDER BY number_occurences DESC