# How to count the nodes group by their properties' values

**URL:** <https://community.neo4j.com/t/how-to-count-the-nodes-group-by-their-properties-values/41732>\
**Category:** Cypher\
**Tags:** apoc\
**Created:** [July 21, 2021, 2:58am UTC](https://community.neo4j.com/t/how-to-count-the-nodes-group-by-their-properties-values/41732 "2021-07-21T02:58:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![changecpl](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/changecpl/32/4087_2.png) [@changecpl](https://community.neo4j.com/u/changecpl)\
**Post date:** [July 21, 2021, 2:58am UTC](https://community.neo4j.com/t/how-to-count-the-nodes-group-by-their-properties-values/41732/1 "2021-07-21T02:58:27Z")

</div>

Hi, everyone!  
I am new to neo4j. I use the procedure _apoc.path.subgraphNodes_ to get all the nodes in a subgraph.

```auto
MATCH (p:Pkg {name: "express", version:'4.17.1'})
CALL apoc.path.subgraphNodes(p, {
	relationshipFilter: "DependOn",
	minLevel:1,maxLevel:10
})
YIELD node
RETURN node

```

However, I want to count these nodes group by a property's values, which named _license_. The output should be like this:

```auto
license count
name1 10
name2 12
... ..

```

I know there is no _group by_ clause in neo4j. What I want to do is like:

```auto
...
RETURN node.license, COUNT(node) GROUP BY node.license

```

Regards  
Changecpl

---

<div class="post-metadata">

**Author:** ![tard\_gabriel](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tard_gabriel/32/28033_2.png) [@tard\_gabriel](https://community.neo4j.com/u/tard_gabriel)\
**Post date:** [July 21, 2021, 7:06am UTC](https://community.neo4j.com/t/how-to-count-the-nodes-group-by-their-properties-values/41732/2 "2021-07-21T07:06:21Z")

</div>

Hi @changecpl

Any Neo4j Staff member is welcome to disagree, maybe I don't understand clearly the need here, but if I do I think you don't need apoc at all in this case and for production, intuitiveness and speed purpose I would recommend to use Cypher only if you can.

APOC is awesome and covers a lot of advanced uses cases, but too many beginners jump way to fast into apoc. The Cypher language is build to be natural, fast and efficient, it's a huge part of the graph philosophy.

MATCH (n)-[:DEPEND\_ON\*1..10]-\>(p:Pkg {name: "express", version:'4.17.1'})  
WITH n.license AS license, count(n) AS count  
RETURN license, count ORDER BY license

Easier to read, write and understand. But might not fit your needs.

You can read more about variable path length [here](https://neo4j.com/docs/cypher-manual/current/syntax/patterns/#cypher-pattern-varlength).

You also need to add a composite index for your query, I will let you dig into the Cypher User manuel or Neo4j academy courses about it.

---

<div class="post-metadata">

**Author:** ![changecpl](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/changecpl/32/4087_2.png) [@changecpl](https://community.neo4j.com/u/changecpl)\
**Post date:** [July 21, 2021, 9:30am UTC](https://community.neo4j.com/t/how-to-count-the-nodes-group-by-their-properties-values/41732/3 "2021-07-21T09:30:23Z")

</div>

Oh, Thanks a lot @tard_gabriel ！You are right. The Cypher language is easy to read ! That is so close. I want to count the nodes that the package **express** depends on both directly and indirectly, group by the property **license**'s values. I change your code, but the count seems contains duplicate nodes  
due to the intersection of different paths. Finally I put the _distinct_ where it should be !  
Thanks bro !

```auto
MATCH (p:Pkg {name: "express", version:'4.17.1'})-[:DependOn*1..]->(n:Pkg)
// due to the intersection of different paths, n contains duplicate nodes, use distinct
WITH n.license AS license, count(distinct n) AS count
RETURN license, count ORDER BY license

```

---

<div class="post-metadata">

**Author:** ![changecpl](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/changecpl/32/4087_2.png) [@changecpl](https://community.neo4j.com/u/changecpl)\
**Post date:** [July 25, 2021, 2:10am UTC](https://community.neo4j.com/t/how-to-count-the-nodes-group-by-their-properties-values/41732/4 "2021-07-25T02:10:25Z")

</div>

Actually I find that **apoc.path.subgraphNodes** is much faster than **MATCH (A)-[:Relationship\*1..]-\>(B)** when the subgraph to search is big... 😦
