15

I'd like a query that counts how many nodes have each label in the dataset. For instance:

LabelA 100 LabelB 200

I can do this for each individual label with something like

MATCH (n:LabelA) return count(n);

But, I'd like to do it for every label in one command.

leadZERO
  • 195
  • 1
  • 1
  • 4

2 Answers2

24

Try something like this

MATCH (n) 
RETURN count(labels(n)), labels(n);

This will return the sum of the labels in the first column and the label name in the second.

Daniel Darabos
  • 26,991
  • 10
  • 102
  • 114
tehAnswer
  • 960
  • 1
  • 13
  • 28
  • That works, a follow-up question, does the distinct modifier apply to just "count(labels(n))" or to the set of "count(labels(n)), labels(n)"? – leadZERO Feb 04 '14 at 15:44
  • I think it applies only to the set of labels, this is, the collection that returns the function labels(n), because count(labels(n)) is an aggregate value. Anyways, maybe the modifier isn't necessary. – tehAnswer Feb 04 '14 at 19:05
  • 1
    `DISTINCT` is redundant here (at least in neo 4.2). – Antoine Apr 07 '22 at 09:39
7

A quick alternative here, for single labels only, APOC Procedures offers a quick means of using the counts store to get the counts:

CALL apoc.meta.stats() YIELD labels
RETURN labels
Petr Matuska
  • 553
  • 5
  • 15
InverseFalcon
  • 29,576
  • 4
  • 38
  • 51