Cypher - 如何通过WITH管道传输变量

时间:2013-08-26 19:52:54

标签: neo4j cypher graph-databases with-clause

我有以下查询:

START e1=node:event(prop="0")
MATCH e1-[r:rbel]->e2
WITH e1, e2, count(e1) as ecount
MATCH e1-[:redge]->p<-[:redge]-e2
WITH p.element_type as Type, p.label as Label, (count(p)*100./ecount) as percentage
WHERE percentage > 20
RETURN Type, Label, ROUND(percentage) as Percentage

我试图计算指定模式在prop="0"的事件中发生的次数与这些事件中发生的所有模式的比例。

我收到以下错误:Unknown identifier 'ecount'

所以我用ecount替换了计算中的count(ecount),并且一直产生100%的percentage,我知道这不是真的。

我是不是错了?如何将ecount的值带到WITH子句并在计算中使用它?

感谢任何帮助!

2 个答案:

答案 0 :(得分:4)

此查询是否适合您?每当我在e1语句中合并count(e1)WITH时,count(e1)始终为1.我认为这是因为count(e1)聚合不再起作用当你选择e1时。您要么忽略e1count(e1)

START e1=node:event(prop="0")
MATCH e1-[r:rbel]->e2
WITH e1, e2
MATCH e1-[:redge]->p<-[:redge]-e2
WITH p.element_type as Type, p.label as Label, (count(p)*100./count(e1)) as percentage
WHERE percentage > 20
RETURN Type, Label, ROUND(percentage) as Percentage

<强>更新 在使用您提供的控制台设置后,我得到了以下查询:

START e1=node:node_auto_index(prop="0") 
MATCH e1-[r:rbel]->e2 
WITH COLLECT(e2) AS e2collection, count(e1) AS cnt 
MATCH e1-[:redge]->p<-[:redge]-(e2) 
WITH p, COLLECT(e1) AS e1collection, cnt, e2collection 
WITH p.name AS Name, cnt, count(p)*100/cnt AS Percentage 
WHERE Percentage > 20 
RETURN Name, Percentage

答案 1 :(得分:1)

h3nrik的解决方案在下面的控制台设置示例中完美运行,但由于某种原因,在应用于我的localhost数据浏览器中的实际数据时,它无法正常工作。尽管查询时间较慢,但我发现了以下解决方法:

START e1=node:event(prop="0") 
MATCH e1-[:rbel]->e2 
WITH count(e1) as ecount
START e1=node:event(prop="0") 
MATCH e1-[:rbel]->e2, e1-[:redge]->p<-[:redge]-(e2)
WITH p.label AS Label, p.element_type as Type, ecount, count(p)*100./ecount AS percentage 
WHERE percentage > 20 
RETURN Label, Type, ROUND(percentage) as Percentage