neo4j - Cypher returning multiple rows per match -


i querying neo4j cypher , getting 2 rows per match:

match (g:group {name: "goliath_treasury237"})-[w:member]->(a:account)<-[y:account]-(p:person)-[manager*0..1]->(b:person)  not p.staffid = b.staffid  match (g:group {name: "goliath_treasury237"})-[j:member]->(v:account)<-[x:account]-(p:person)-[f:dep*0..1]->(d:department)  return  p.givenname, p.surname, p.staffid, p.corpt, b.staffid, d.name 

i'm trying information both department , boss of person i'm struggling declaratively unless double match. returns 2 rows each match person has boss, 1 id bosses id , 1 correct bosses id. people without boss 1 row bosses id own.

if remove variable length path boss 1 row each individual no row doesn't have boss.

i'm @ loss now, great!

there's several things can fix here.

for one, don't need second match start on beginning, can reuse same p node without needing stuff before. don't need have variable on every node , relationship if you're not going use or return in way.

you can use optional match when don't want match filter out rows, newly-introduced variables in optional match null if match fails.

something should work:

match (:group {name: "goliath_treasury237"})-[:member]->(:account)<-[:account]-(p:person) optional match (p)-[manager]->(b:person)  not p.staffid = b.staffid  optional match (p)-[:dep]->(d:department)  return  p.givenname, p.surname, p.staffid, p.corpt, b.staffid, d.name 

Comments