Michael Ryan
Michael Ryan

Reputation: 73

Cypher Query - Excluding certain relationships

I am querying my graph where it has the following nodes:

With the following relationships:

The query I am trying to achieve is returning all Customers that have accounts that hold Microsoft through a fund. The following is my query:

MATCH (c:Customer)-[h:HAS]->(a:Account)-[p:PURCHASED]-(f:Fund)-[holds:HOLDS]->(s:Stock {ticker: 'MSFT'})
WHERE exists((f)-[:HOLDS]->(s:Stock))
AND exists ((f:Fund)-[holds]->(s:Stock)) 
AND NOT exists((a:Account {account_type: 'Individual'})-[p:PURCHASED]->(s:Stock))
RETURN *

This almost gets me the desired results but I keep getting 2 relationships out of the Microsoft stock that is tied to an Individual account where I do not want those included.

Any help would be greatly appreciated!

Result:

enter image description here

Desired Result:

enter image description here

Upvotes: 0

Views: 62

Answers (2)

stdob--
stdob--

Reputation: 29172

It seems to me that you should just uncheck the "Connect result nodes" option in the Neo4j Browser:

enter image description here

Upvotes: 1

jose_bacoy
jose_bacoy

Reputation: 12684

There is duplications in your query. Lines 2 and 3 are the same. Line 2 is a subgraph of Line 1. Then you are using the variables a, p and s more than once in line 1 and line 4. Below query is not tested but give it a try. Please tell me if it works for you or not.

MATCH (c:Customer)-[h:HAS]->(a:Account)-[p:PURCHASED]-(f:Fund)-[holds:HOLDS]->(s:Stock {ticker: 'MSFT'})
WHERE NOT exists((:Account{account_type: 'Individual'})-[:PURCHASED]->(:Stock))
RETURN *

Upvotes: 1

Related Questions