Catalin
Catalin

Reputation: 23

neo4j union - post processing results

I need a query that checks if a node exists, a specific attribute on that node exists and post-process the result using case or something.

Ex:

CALL apoc.cypher.run("OPTIONAL MATCH (n:Location{SureName:'9144735079d813886326'}) RETURN CASE n.SubType WHEN null THEN 'Location was not loaded' ELSE n.SubType END AS result UNION OPTIONAL MATCH (n:Location{SubType:'Site',SureName:'914473507981388d6326'}) RETURN CASE n.SubType WHEN null THEN 'NotLoaded' ELSE n.SubType END as result", null) YIELD value AS rv

But I want to be able to return just one string depending on the results that I get.

Thanks.

Upvotes: 1

Views: 92

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30397

You should be able to use a CASE statement to output the value based upon conditions. Maybe a query like this might work:

// assume you've passed in $expectedSubType as a parameter
OPTIONAL MATCH (n:Location{SureName:'9144735079d813886326'}) 
WITH n, n IS NULL as notLoaded
RETURN CASE WHEN notLoaded THEN 'Location was not loaded' 
            WHEN n.SubType IS NULL THEN 'SubType missing'
            WHEN n.SubType = $expectedSubType THEN 'Expected value'
            ELSE 'Different value: ' + n.SubType END AS result

Upvotes: 1

Related Questions