CEORiley
CEORiley

Reputation: 1

Neo4j Cypher WITH clause

I'm going through the following exercise:

What is average number of reviews for all users? (enter an integer number)

The following is the solution which produces the correct answer of 149:

        MATCH (u:User)-\[r:RATED\]-(m:Movie)
        WITH u, count(r) AS NumReviews
        WITH collect(NumReviews) AS ReviewCounts
        UNWIND ReviewCounts AS x
        RETURN toInteger(avg(x))

My basic understanding of the WITH clause is the ability to either bring a variable in scope to be used in the next transaction or to introduce new variables from the result of an expression. The question has to do with the 'u' variable in the first WITH clause. I don't see where it's being used at all. Yet, if I remove it and rerun the query, the result changes to 100004 which is incorrect. Can someone explain the importance of the 'u' variable in the WITH clause? Thanks

Upvotes: 0

Views: 69

Answers (1)

Finbar Good
Finbar Good

Reputation: 1461

Any terms in the WITH clause that are not aggregation functions are used as grouping keys. So including u will group the counts by User, but without will count Movies rated by all Users.

Upvotes: 0

Related Questions