David
David

Reputation: 16120

How to group paths by nodes or node labels

Given a cypher query such as the following:

match a = (bs:BodyStructure {Name:"Brain structure"})<-[fs:Finding_site]-(dis:Disorder)-[r:Causative_agent]->(p) return a

How might I modify it to return a count of paths for each p, or the count of paths for each label of p?

(In this example, p can be labelled as either Substance or Organism.)

Upvotes: 0

Views: 135

Answers (1)

cybersam
cybersam

Reputation: 66999

To find the number of paths for each p node:

MATCH (:BodyStructure {Name:"Brain structure"})<-[:Finding_site]-(:Disorder)-[:Causative_agent]->(p)
RETURN p, COUNT(*)

To find the number of paths for each p label (I assume there can be at most one label for the p nodes):

MATCH (:BodyStructure {Name:"Brain structure"})<-[:Finding_site]-(:Disorder)-[:Causative_agent]->(p)
RETURN labels(p)[0] AS label, COUNT(*)

In the above RETURN statements, COUNT(*) is simply counting the number of matched rows that have the given aggregate key (p or label).

Upvotes: 1

Related Questions