MATCH Between a pair of nodes with a relationship in common AND holding another kind of relationship NOT with the same Node

Hi ,

Below is the use case I am stuck with :
There are 3 entities Author,Paper,Affiliation and their relationships are (:Author)-[:WROTE]-(:Paper) and (:Author)-[AFFILIATED_WITH]-(:Affiliation)
I have created a relationship [:COAUTHOR] if two authors has a paper in common.I need the authors who are having [COAUTHOR] relationship but affiliated to different institute.I have tried couple of queries but am getting authors who are COAUTHORS with no affiliation at all:
MATCH (a:Author)-[:COAUTHOR]- >(a1:Author),(afl:Affiliation) WHERE NOT
(a)-[:AFFILIATED_WITH]- >(afl)< -[:AFFILIATED_WITH]-(a1)
RETURN a,a1,afl LIMIT 25
MATCH (a:Author)-[:COAUTHOR]- >(a1:Author),(afl:Affiliation) WHERE size((a)-[:AFFILIATED_WITH]- >(afl)< -[:AFFILIATED_WITH]-(a1)) = 0 RETURN a,a1,afl LIMIT 25

Ah, I think I see the problem.

To ensure that each author has at least one :Affiliation relationship, you can add a check for that pattern for each author:

WHERE (a)-[:AFFILIATED_WITH]-() AND (a1)-[:AFFILIATED_WITH]-()

Also I'd remove the (afl:Affiliation) as a part of the match pattern itself, that's generating a cartesian product of your coauthor results against every :Affiliation node. Instead add the :Affiliation label into the pattern of your WHERE clause (and remove usage of afl).

Unless you're trying to generate this for every :Affiliation they are explicitly not commonly affiliated with.

Thanks Andrew ,
I got the expected result with modification you suggested -

MATCH (a:Author)-[:COAUTHOR]- >(a1:Author) WHERE NOT
(a)-[:AFFILIATED_WITH]- >(:Affiliation)< -[:AFFILIATED_WITH]-(a1) and (a)-[:AFFILIATED_WITH]-() and (a1)-[:AFFILIATED_WITH]-()
RETURN a,a1 LIMIT 25