neo4j/neo4j — pull request #1366 by systay (2013-10-27 15:18:17+00:00)
Title: Allows aggregate expressions to be part of more complicated expressions 

Cypher calculates what is the aggregating key and which is the aggregating expression. For most cases, this works well. When a single expression contains both the key and the aggregation, a little more work is needed.

This PR changes Cypher's handling of these composite expressions so it does something like this:

MATCH n
RETURN n.foo + count(*)

This first query contains an expression in the RETURN clause that contains both the aggregate expression ( count(*) ) and the aggregate key ( n.foo ). To handle this, Cypher rewrites the query so it looks like this:

MATCH n
WITH n.foo as X1, count(*) as X2
RETURN X1+X2 as `n.foo + count(*)`

The queries are equivalent, but much easier for Cypher to handle.