Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NEO4j Cypher query returning distinct value

Tags:

neo4j

cypher

hello guys I am new to cypher query and I want to restrict duplicate values in my result

the query is

match (ee:Personal {id:"id"})-[:Friend]->(fr),
 (fr)-[:Friend]->(fr2),
 (fr2)-[:Friend]->(friend:Personal),
 (friend)-[:Works]->(comp:Company) 

where comp.name=~".*name.*" 
and not friend.id="id" 
and not (friend)-[:Friend]-(fr) 
and not (friend)-[:Friend]-(ee) 
and not (fr2)-[:Friend]-(ee) 

optional match (comp)-[:Position]->(pos), 
 (friend)-[:Position]->(pos)  
optional match (friend)-[:Location]->(loc) 

return distinct  friend.name,  comp.name

but i get duplicate values as there are multiple node properties in return statemnt. If I return only one property then its fine. but i want distinct friends

like image 573
Hussain Avatar asked Nov 30 '22 11:11

Hussain


1 Answers

Distinct works on the whole row, if you want to return distinct friends per company, do:

return comp.name, collect(distinct friend.name)
like image 60
Michael Hunger Avatar answered Dec 09 '22 07:12

Michael Hunger