cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Different MATCH due to earlier results

Depending on the result reported from one MATCH I would like to have different WHERE clauses. Is that possible?
A simple case is that if the first MATCH returns a null then I want to have one MATCH with WHERE and if it is not null I want a different MACTH,

An alternative is of course to do it in two seperate cypher queris and check the result from the first

7 REPLIES 7

Benoit
Graph Buddy

You can do it with APOC, there is the procedure apoc.case (read only queries) & apoc.do.case (read & write queries) that do exactly what you want : https://neo4j-contrib.github.io/neo4j-apoc-procedures/#_case_procedures

You need to use OPTIONAL MATCH instead of MATCH if you want the results to include null values. After OPTIONAL MATCH you can use either APOC or a CASE plus UNWIND or FOREACH statements.

In any case a more concrete example would be helpful to ensure we're offering appropriate advice.

Might not be 100% syntacticaly correct but my orignail cypher was something like this

MATCH (f:F) --> (n:S ) MATCH (f:F) --> (p:P) MATCH (p:P)-->(t:T) where (( t.date <= f.date) RETURN t.data

But if f.date is equal "" then I would like to modify it to this

MATCH (f:F) --> (n:S ) MATCH (f:F) --> (p:P) MATCH (p:P)-->(t:T) RETURN t.data

The only difference between your two queries is the WHERE clause no ? Results are the same.

So you can achieved it just by adding a predicate in your WHERE clause like that :

MATCH (f:F)-->(n:S ), 
      (f)-->(p:P),
      (p)-->(t:T)
WHERE 
      (NOT f.date = "" AND (t.date <= f.date)) OR
      (f.date = "")
RETURN t.data

I tried to shorten my original cypher. This is a longer version of the WHERE clause

(( t.date <= f.date) AND ( NOT t.date = '' AND NOT t.date is null))

Ok, but it's the same thing, a OR in your WHERE clause is sufficient :

WHERE 
      (NOT f.date = "" AND t.date <= f.date  AND NOT t.date = '' AND NOT t.date is null) OR
      (f.date = "")