user3066588
user3066588

Reputation: 111

neo4j distinct two columns

How to return two different columns with cypher in Neo4j? The query which I've got is that:

MATCH (a:Person)-[r:WorksFOR]->(b:Boss), (c:Boss)<-[r2:WorksFOR]-(d:Person)
WHERE  b.sex = c.sex
RETURN a,  d;

And it returns:

a              d
John           Will
Will           John

I want to get rid of one of the column.

Upvotes: 2

Views: 2920

Answers (1)

FrobberOfBits
FrobberOfBits

Reputation: 18022

OP needs to reword the question to clarify OP wants to get rid of one of the rows.

Here's a query that does that:

MATCH (a:Person)-[r:WorksFOR]->(b:Boss), (c:Boss)<-[r2:WorksFOR]-(d:Person)
WHERE  b.name < c.name AND 
       b.sex = c.sex AND
       b <> c
RETURN a,  d;

The problem with your query is that b and c can match any Boss. To force them to match in one order, I've added b.name < c.name. The order doesn't matter, this just forces it to match one way, but not the other. I've added b <> c because you have to handle the case where they work for the same boss, which I don't think you want.

Once you add the ordering, the boss matching (b and c) can only happen one way, not the other way, so your second row of results gets eliminated.

Upvotes: 3

Related Questions