I have been experimenting with neo4j and cypher lately but I don't quite manage to get the following query correctly. I would like to return all employees that share at least one project with a given employee and the projects they worked on. It is perhaps simpler if described with an example:
The desired outcome for the graph above for the input being employee 1 is:
| Employee | Project |
|---|---|
| 0 | A |
| 1 | A |
| 1 | B |
| 4 | A |
I tried the following query but it returns duplicated relationships:
MATCH (a0:Employee {name:1})-[:WORKS]->(b0:Project)
MATCH (b0)<-[:WORKS]-(a:Employee)
MATCH (a)-[:WORKS]->(b:Project)
RETURN a.name AS employee, b.name AS project
ORDER BY employee, project
| Employee | Project |
|---|---|
| 0 | A |
| 1 | A |
| 1 | A |
| 1 | B |
| 1 | B |
| 4 | A |
Thank you in advance for your help.
Note: These queries can be used to create the graph above
CREATE (:Employee {name: 0}),
(:Employee {name: 1}),
(:Employee {name: 2}),
(:Employee {name: 3}),
(:Employee {name: 4}),
(:Project {name: 'A'}),
(:Project {name: 'B'}),
(:Project {name: 'C'})
MATCH (e:Employee), (p:Project) WHERE e.name=0 AND p.name='A' CREATE (e)-[:WORKS]->(p)
MATCH (e:Employee), (p:Project) WHERE e.name=1 AND p.name='A' CREATE (e)-[:WORKS]->(p)
MATCH (e:Employee), (p:Project) WHERE e.name=1 AND p.name='B' CREATE (e)-[:WORKS]->(p)
MATCH (e:Employee), (p:Project) WHERE e.name=4 AND p.name='A' CREATE (e)-[:WORKS]->(p)
MATCH (e:Employee), (p:Project) WHERE e.name=3 AND p.name='C' CREATE (e)-[:WORKS]->(p)
