Understanding WITH

19 views
Skip to first unread message

Johnny Weng Luu

unread,
May 5, 2012, 12:14:22 PM5/5/12
to ne...@googlegroups.com
I can't understand this example from the doc (http://docs.neo4j.org/chunked/snapshot/query-with.html):

START david=node(1)
MATCH david--otherPerson-->()
WITH otherPerson, count(*) as foaf
WHERE foaf > 1
RETURN otherPerson

What does otherPerson and count(*) do in the WITH line?

Johnny

Michael Hunger

unread,
May 5, 2012, 2:30:56 PM5/5/12
to ne...@googlegroups.com
Johnny,

what you see here are two queries joined by with.
With acts as a return for the first query and as "start" of the second one.
It set's the part of the context that is passed on.

normally you would have
START david=node(1)
MATCH david--otherPerson-->()
RETURN otherPerson, count(*) as foaf

and then do the filtering in your calling code.

with with you can just start the next query directly in cypher, which can only see what is declared in the with section, 
in this case: otherPerson and foaf the other identifiers and data of the first query are no longer available.

HTH

Michael

P.S: If this helped you in understanding, please repost the question at StackOverflow then I can put my answer there as well, so more people can learn from it. Thanks

Johnny Weng Luu

unread,
May 5, 2012, 4:12:55 PM5/5/12
to ne...@googlegroups.com
Thanks,

I get the concept now.

May I ask what count(*) does?

Johnny

Michael Hunger

unread,
May 5, 2012, 4:32:15 PM5/5/12
to ne...@googlegroups.com
what you do is aggregating

normally this query would return otherPerson many times (as often as it appears in a path from david to a fried of friend)

using otherPerson in return or with together with aggregation functions like count, min,max,avg,collect makes it an aggregation (like the SQL group by)
so it returns otherPerson just once and pulls all the other values through the aggregation functions and returns the result of all the other values _per_ otherPerson
count(*) just counts the number of subgraphs aka "rows" you can also use something like count(distinct friend_of_friend)

Michael

Johnny Weng Luu

unread,
May 5, 2012, 8:08:41 PM5/5/12
to ne...@googlegroups.com
Thanks for the info.

I get it now!

I posted the question here:


Waiting for you response :)

Johnny
Reply all
Reply to author
Forward
0 new messages