jobbietronic
jobbietronic

Reputation: 65

Comparing node counts in Cypher / Neo4J in a single query

I'm trying to shape a cypher query to select a count of nodes and then count the percentage of those nodes that have links of a specific type. Essentially How many appointments did this doctor attend and how many of those resulted in an Arthritis Diagnosis?

So I start with a count of all the nodes

    (d:Doctor)-[ATTENDED]-(apt:Appointment)
    RETURN d.name, count(apt)

Then I want to compare the above count with the number that satisfy this pattern

    (d:Doctor)-[*1..2]-(c:Condition {desc:'Arthritis'})
    RETURN d.name, count(c)

I know I'm being dumb and there must be a simple way to shape the results using UNION or WITH but my attempts have resulted in really long execution times with wrong results!

I'd like to see a table with something like the below...

Dr:Dr Zhivago, Total:850, Arthritis:8%

Thanks

Upvotes: 2

Views: 544

Answers (2)

alacambra
alacambra

Reputation: 577

I will try to figure the modele out.

match 
(c:Doctor)-[ATTENDED]->(apt:Appointment),
(apt)-[*0..1]->(d:Condition {desc:'Arthritis'}) 
with c as consultant, count(c) as appointments, count(d) as arthritis
return consultant.name as name, appointments as total, arthritis/appointments * 100 as arthritis 

Hope that helps.

Upvotes: 1

jobbietronic
jobbietronic

Reputation: 65

Writing the question helped!

MATCH (c:Doctor)-[*0..2]-(d:Condition {desc:'Arthritis'})
WITH c AS doctor, count(d) AS arthritis_count
MATCH (doctor)-[]-(v:Appointment)
RETURN consultant.name, arthritis_count, count(v)

Upvotes: 1

Related Questions