Search code examples
neo4jneo4j-apoc

neo4j get values that match a criteria


I'm working on a neo4j query trying to return a node that has a total ownership of 1 (representing 100%). So I'm summing the ownership of a node. My query to get the sum of ownership works correctly and is as follows:

match (o:owner)-[:mineral_interest_owner]->(w:well) return sum(o.ownership)

So then I try to use that in an expression :

match (o:owner)-[:mineral_interest_owner]->(w:well) return sum(o.ownership) case when sum(o.ownership) = 1 then w.name else "None"

and get this error: Neo.ClientError.Statement.SyntaxError: Invalid input 's': expected 'l/L' (line 3, column 3 (offset: 78)) "case" ^

Is this something that I need to use APOC for?


Solution

  • Aside from your immediate question, your data model seems to need improvement. The ownership property should not be on the owner node, since that would mean that the owner has the same exact ownership percentage for all wells in which s/he has an interest. Instead, the ownership property should be on the mineral_interest_owner relationship -- which would allow all ownership percentages to be different.

    Assuming that you incorporate the above data model improvement into your DB, this query should return all owner/well pairs where the owner owns 100% of that well.

    MATCH (owner:owner)-[i:mineral_interest_owner]->(well:well)
    WITH owner, well, SUM(i.ownership) AS pct
    WHERE pct = 1
    RETURN owner, well;