cancel
Showing results for 
Search instead for 
Did you mean: 

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(sd.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.

1 ACCEPTED SOLUTION

Cobra
Ninja
Ninja

Hello @nswamy.lakshman and welcome to the Neo4j community

For each year of your database:

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

Between 2016 and 2020:

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

OR

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

Regards,
Cobra

View solution in original post

4 REPLIES 4

Cobra
Ninja
Ninja

Hello @nswamy.lakshman and welcome to the Neo4j community

For each year of your database:

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

Between 2016 and 2020:

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

OR

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

Regards,
Cobra

appreciate your prompt reply.

Cobra
Ninja
Ninja

Does it solve your problem?

yes.. does address my needs.