cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Cypher Query to Count the Number of Properties based on a Condition

OmShanti
Node Clone

Hi,

I have a Label named 'City' assigned to 10 nodes i.e. City1, City2, City3.... City10.

Each of these 10 nodes have properties like Park, Market, School, Hospital, Court, Population etc but having different values, like for some cities if there are no Hospital & Park then their properties are defined as:

Park = "no", Market ="yes", School="yes", Hospital="no", Court = "yes", Population= 100000

Similarly, if for some cities there are no Market, Hospital, Court then

Park = "yes", Market ="no", School="yes", Hospital="no", Court = "no", Population= 5000

So, basically, the idea is each city has different properties values.

My objective is, from among these 10 nodes, i need to select a given city & calculate a metric based on below steps:
Step1: First Count those properties for which value is "yes" (Similar to COUNTIF in Excel)
Step2: Additionally, check whether population of these cities is greater than
some threshold(say, 3000) and then
Step3: Use Count from Step1 and result from Step2 to calculate a metric(some math operations)

Can you please share how can we build such a Cypher Query.

2 REPLIES 2

The population check is easy, just use an expression with the inequality that will result in a boolean.

For the others, if you know the properties, you can project those into a list and use a filter operation to only retain the ones that are "yes" then get the size of the list as the number of "yes" properties:

MATCH (c:City)
WITH c.Population > 3000 as meetsPopThreshold, [val in [c.Park, c.Market, c.School, c.Hospital, c.Court] WHERE val = "yes"] as yesProps
WITH meetsPopThreshold, size(yesProps) as yesCount
... // now do whatever calculations you need to do with these

Thanks @andrew.bowman for the response. It works!