Cypher:如何在同一个查询中执行COUNT和MAX / MIN(相同的聚合)

时间:2013-09-10 17:30:24

标签: neo4j cypher

我的模特是这样的:     (wine_critic) - [REVIEW] - GT;(wine_bottle)

wine_critic可以对同一wine_bottle进行多次评论, (他们不知道,它是盲品) 每个评论都有时间戳和分数

所以,我想得到一个wine_critic审查wine_bottle的评论总数,以及同一评论家给同一瓶子的MAX / MIN评分。

START critic=node:critics('ID:5')
MATCH (critic)-[rev:REVIEW]->(bottle)
WITH critic, bottle, COUNT(rev) as total_reviews
RETURN critic.name, bottle.name, total_reviews

但是,在同一个查询中我想要MAX(rev.score)和MIN(rev.score)。

有可能吗?

1 个答案:

答案 0 :(得分:2)

在这种情况下你真的不需要WITH:

START critic=node:critics('ID:5')
MATCH (critic)-[rev:REVIEW]->(bottle)
RETURN critic.name, bottle.name, COUNT(rev) as total_reviews, MAX(rev.score) as max, MIN(rev.score) as min

也可以同时抛出AVG(rev.score)STDEV(rev.score)

更新: 1.8.x中的丑陋解决方法:

START critic=node:critics('ID:5')
MATCH (critic)-[rev:REVIEW]->(bottle)
WITH critic, bottle, MIN(rev.score) as min
MATCH (critic)-[rev:REVIEW]->(bottle)
RETURN critic.name, bottle.name, COUNT(rev) as total_reviews, MAX(rev.score) as max, min