Different MATCH due to earlier results

cypher

(Andersoxie) #1

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


(Benoit Simard) #2

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


(Bratanic Tomaz) #3

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.


(Andrew Bowman) #4

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


(Andersoxie) #5

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


(Benoit Simard) #6

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

(Andersoxie) #7

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))


(Benoit Simard) #8

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 = "")