Count Relationships per Row?

We have users, groups and a third thing (measurements) in our database.

User can be members of one or more groups.

(User) - (Measurement)

Now I need to do something with the Measurement, dependent on the number of groups of which the user is member of.

How can I count the number of relationships of users per row? All my tries failed so far and do a database wide aggregation of these relations.

The situation is a bit more complicated, because users usually have two relationships/edges to the groups.

     /        \
    |[:a]    |[:b]
     \        /

My Code so far:

MATCH (u:User)-[mRel:IS_MEMBER]->(hh:Household) RETURN COUNT(DISTINCT mRel) AS groups;
 // gives
| groups |
| 13     |

// CASE COUNT(mRel) WHEN 1 THEN 1 ELSE COUNT(mRel) END as result

Using an UNWIND on collected users does not help, using paths does not help, too, because two groups are of cause not part of the same path.

Another example:

MATCH (u:User) WITH ID(u) AS userId MATCH (user:User)-[r:IS_MEMBER]->(h:Household) WHERE ID(user) = userId RETURN COUNT(r) as groups

gives the same result.

So COUNT seems to be the wrong approach, but I cannot see an alternative for counting relationships of nodes in a row yet.

If I have not misunderstood,
it would be enough to add the User node to the return of your query.

MATCH (u:User)-[mRel:IS_MEMBER]->(hh:Household) return COUNT(DISTINCT mRel) as count, u

in this way you group count per User node,
so you could do for example:

MATCH (u:User)-[mRel:IS_MEMBER]->(hh:Household) WITH COUNT(DISTINCT mRel) as count, u
RETURN case count when 1 then "first thing" else "second thing" end as result
1 Like