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.

Can someone help me with this query please?

3bfred
Node Link

I am a newcomer to Neo4J but have been making good progress. I need some help with this one though.

The following query is supposed to return me 2 independent counts in a single query but returns pcount=0 and gcount=0 when I am looking for pcount=1 and gcount=0.

If I run each "half" of the query individually, I get the correct results.

// Person

MATCH (p:Person)
WHERE p.lat > -90
WITH p
MATCH (p:Person)-[:SKILL]-(skill:Skill) WHERE skill.skillId IN ['instrument_accompanist']
WITH p
// If I RETURN count(p) here, I get '1' as expected.
// Gig (should return nothing)
MATCH (g:Gig)
WHERE g.lat > -90
WITH g, p
MATCH (g:Gig)-[:SKILL]-(skill:Skill) WHERE skill.skillId IN ['instrument_accompanist']
WITH p, g
RETURN count(p) as pcount, count(g) as gcount
 
I believe that the cartesian product is empty, but I can't figure out using collect/distinct etc how on earth to solve it.

 

1 ACCEPTED SOLUTION

Try this:

call{
    MATCH (p:Person)
    WHERE p.lat > -90
    AND EXISTS {
      (p)-[:SKILL]-(skill:Skill)
       WHERE skill.skillId IN ['instrument_accompanist']
    }
    RETURN 'person' as type, count(p) as count

    UNION ALL

    MATCH (g:Gig)
    WHERE g.lat > -90
    AND EXISTS {
      (g)-[:SKILL]-(skill:Skill) 
      WHERE skill.skillId IN ['instrument_accompanist']
    }
    RETURN 'gig' as type, count(g) as count
}
return type, count

View solution in original post

3 REPLIES 3

Try this:

call{
    MATCH (p:Person)
    WHERE p.lat > -90
    AND EXISTS {
      (p)-[:SKILL]-(skill:Skill)
       WHERE skill.skillId IN ['instrument_accompanist']
    }
    RETURN 'person' as type, count(p) as count

    UNION ALL

    MATCH (g:Gig)
    WHERE g.lat > -90
    AND EXISTS {
      (g)-[:SKILL]-(skill:Skill) 
      WHERE skill.skillId IN ['instrument_accompanist']
    }
    RETURN 'gig' as type, count(g) as count
}
return type, count

3bfred
Node Link

Thanks a lot for this - much appreciated. Will try out asap. 

I really love Neo4J but Cypher can be a real ballache. I have not learned Neo4J systematically (as I know I should have) but I can't help wondering if it isn't possible to make it semantically more approachable for many usual use cases.

I felt overwhelmed at first. I plowed along reading the cypher manual over and over and trying a lot of the features. I also suggest you take the graph academy classes on cypher.