Cypher for counting node property that meet certain conditions

I have several nodes of the following type: label - ServiceDate, property - date
(:ServiceDate {date: "2018-02-13"} )

I like to write a query that returns the distinct years and number of nodes in each year. The query should return the following:

2016 2017 2018 2019 2020
100 30 200 300 240

the following query gives me the required detail for a particular year :

MATCH (sd:ServiceDate)
WITH date( as sd1
WHERE sd1.year = 2018
RETURN count(sd)

I need help on the query that will return for all the years from 2016 to 2020.

appreciate any help.

Hello @nswamy.lakshman and welcome to the Neo4j community :slight_smile:

For each year of your database:

MATCH (sd:ServiceDate)
RETURN date( AS year, count(sd) AS nbNodes

Between 2016 and 2020:

UNWIND range(2016, 2021) AS year
MATCH (sd:ServiceDate)
WHERE date( = year
RETURN year, count(sd) AS nbNodes


MATCH (sd:ServiceDate)
WITH date( AS year, count(sd) AS nbNodes
WHERE year IN range(2016, 2021)
RETURN year, nbNodes


appreciate your prompt reply.

Does it solve your problem?

yes.. does address my needs.