Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
07-03-2020 02:29 AM
I want to figure out the count of nodes with each property. Something like
MATCH (a:car)
RETURN count(a.property="big"), count(a.property="small")
Solved! Go to Solution.
07-03-2020 03:14 AM
There is this way:
MATCH (a:car)
WITH collect(a) AS a
WITH [val IN a WHERE val.property = "big"] AS big, [val IN a WHERE val.property = "small"] AS small
RETURN size(big) AS big, size(small) AS small
If you are using the latest version of Neo4j (4.1), try:
MATCH (a:car)
CALL {
WITH a
WHERE a.property = "big"
RETURN "big" AS p, count(a) AS c
UNION ALL
WITH a
WHERE a.property = "small"
RETURN "small" AS p, count(a) AS c
}
RETURN p, c
07-03-2020 02:43 AM
07-03-2020 03:13 AM
I stand corrected, however I'd wish there was an easier and more intuitive function.
07-03-2020 03:14 AM
There is this way:
MATCH (a:car)
WITH collect(a) AS a
WITH [val IN a WHERE val.property = "big"] AS big, [val IN a WHERE val.property = "small"] AS small
RETURN size(big) AS big, size(small) AS small
If you are using the latest version of Neo4j (4.1), try:
MATCH (a:car)
CALL {
WITH a
WHERE a.property = "big"
RETURN "big" AS p, count(a) AS c
UNION ALL
WITH a
WHERE a.property = "small"
RETURN "small" AS p, count(a) AS c
}
RETURN p, c
07-03-2020 03:22 AM
@alec.wang, I updated my previous answer with a version that could work on Neo4j 4.1 if you prefer it
07-03-2020 05:19 AM
Oh this was great. I'm wondering then if I'm able to do other stuff with the listed nodes like I'm now trying to access their properties kind of like
MATCH (a:car)
WITH collect(a) AS a
sum[val.engine_size in a where val.property="big"] as total_big_engine_capacity
07-03-2020 05:25 AM
Yeah, you can
Since val
is a node (Car), you can get what you want by iterating on it , you can even create a dictionnary of values if you want to get several properties
07-06-2020 06:29 AM
To bypass the collect(a)
You could also write it this way.
MATCH (a:car)
WITH
CASE WHEN (a.property = 'big') THEN 1 ELSE 0 END as isBig,
CASE WHEN (a.property = 'small') THEN 1 ELSE 0 END as isSmall
RETURN sum(isBig), sum(isSmall)
All the sessions of the conference are now available online