Cypher Remove Null S from Result

Cypher Remove Null S from Result

I've created a query in neo4j :

MATCH (x:Node)-[r*1..2]->(y:Node) 
WITH x AS x, SIZE(COLLECT(DISTINCT y)) AS y
WITH CASE
    WHEN y=10 THEN x.target
END AS l
return l AS target

But returns something like :

target
____
null
null
"test1"
null
null
"tes56"
...

What I want is :

target
____
"test1"
"tes56"
...

no null in entries. How can I achieve this ?

1 Answer

You used CASE/WHEN (which does not filter out rows) when you should have been using WHERE.

Also, size(collect(DISTINCT )) is really just a count(DISTINCT ) aggregation, so use that instead.

Also to avoid confusion, maybe use a different variable name for the count result.

MATCH (x:Node)-[*1..2]->(y:Node) 
WITH x, count(DISTINCT y) AS yCount
WHERE yCount = 10
RETURN x.target AS target

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.