Re: How to join two query results into a single one?

55 views
Skip to first unread message

Florian B.

unread,
Jan 22, 2013, 9:21:13 AM1/22/13
to ne...@googlegroups.com
I think you can work with optional relationship here:

START me = node(1) MATCH me-[:KNOWS*0..1]-friend-[:UPDATES_STATUS]-> statusUpdate RETURN statusUpdate


Regards,
Florian


On Tuesday, January 22, 2013 3:03:26 PM UTC+1, Sarpdoruk Tahmaz wrote:

Assume that my project is Facebook. I want to display a feed which consists of my status updates and my friends' status updates both.

Here are the relations;

  • user KNOWS user
  • user UPDATES_STATUS status

This is how I get my friends status updates;

START me = node(1) MATCH me-[:KNOWS]-()-[:UPDATES_STATUS]->friendsStatusUpdates RETURN friendsStatusUpdates

And this is how I get my own status updates;

START me = node(1) MATCH me-[:UPDATES_STATUS]->myStatusUpdates RETURN myStatusUpdates

Both queries work fine but I need a single query that combines these two.

Here is the StackOverflow link for the same question.


Thanks in advance.

Message has been deleted

Sarpdoruk Tahmaz

unread,
Jan 22, 2013, 4:57:02 PM1/22/13
to ne...@googlegroups.com
I did not realize that this query gives duplicates, it works well but provides duplicates in return.  I guess, firstly it finds 0 level deep nodes then it traverses whole graph one more to find at most 1 level deep nodes.  Here is the problem; at most 1 level deep means it includes 0 level deep nodes too so at the end there are 2 sets of 0 level deep nodes.  I added DISTINCT to get rid of duplicates and it worked just fine, but if there are any other solution to duplicates without using duplicated I'd appreciate that too.

START me = node(1) MATCH me-[:KNOWS*0..1]-friend-[:UPDATES_STATUS]-> statusUpdate RETURN DISTINCT statusUpdate


Thanks.

Florian B.

unread,
Jan 23, 2013, 3:25:09 AM1/23/13
to ne...@googlegroups.com
Mh what you can do is separate you query in two parts. First part get all nodes you want to check the status of, me and my friends. In the second part you get the status updates.

START me = node(1)
MATCH
me-[:KNOWS*0..1]-friend
WITH distinct friend
MATCH
friend-[:UPDATES_STATUS]-> statusUpdate
RETURN DISTINCT
statusUpdate

With the WITH statement you define which variables of the previous part should be used in the next part.  

Sarpdoruk Tahmaz

unread,
Jan 23, 2013, 5:25:03 AM1/23/13
to ne...@googlegroups.com
That worked just fine.  Thanks for the alterative solution using WITH statement.
Reply all
Reply to author
Forward
0 new messages