OPTIONAL MATCH (orig)-[:DEPENDS_ON]->(common:Version)<-[:DEPENDS_ON]-(other) WITH orig, other, collect(DISTINCT common.uId) AS common OPTIONAL MATCH (orig)-[:DEPENDS_ON]->(origVersion:Version)-[:VERSION_OF]->(commonArtifact:ArtifactElement), (other)-[:DEPENDS_ON]->(otherVersion:Version)-[:VERSION_OF]->(commonArtifact) WITH orig, other, common, collect({origVersion: origVersion.uId, otherVersion: otherVersion.uId}) AS commonArtifacts OPTIONAL MATCH (orig)-[:DEPENDS_ON]->(origOnly:Version)-[:VERSION_OF]->(a:ArtifactElement) WHERE NOT (other)-[:DEPENDS_ON]->(:Version)-[:VERSION_OF]->(a) WITH orig, other, common, commonArtifacts, collect(DISTINCT origOnly.uId) AS origOnly OPTIONAL MATCH (other)-[:DEPENDS_ON]->(otherOnly:Version)-[:VERSION_OF]->(a:ArtifactElement) WHERE NOT (orig)-[:DEPENDS_ON]->(:Version)-[:VERSION_OF]->(a) RETURN common, commonArtifacts, origOnly, collect(DISTINCT otherOnly.uId) AS otherOnly