Question

Take for example the following scenario:

A hospital node is related to many doctor nodes. Doctor nodes are related to other doctors representing when they refer a patient to another doctor. These referrals can be to a doctor at a different hospital. The relationship between doctors has a property called count that indicates how many times that doctor has referred any of their patients to that second doctor.

What I'm trying to do is take these referral relationships and look at it from the hospital level. My query currently looks like this:

START hospital1 = Node:Hospitals("*:*")
MATCH (hospital1)-[:CHILD_DOCTOR]->(doctor1)-[referral:REFERRED]-(doctor2)<-[:CHILD_DOCTOR]-(hospital2)
WHERE hospital1 <> hospital2
RETURN DISTINCT hospital1, hospital2, referral.count

It sounds complicated but the query is pretty straight forward. Here's the only problem. Let's say two doctors from Hospital A refer a patient to two doctors from Hospital B, I'll end up with two records where hospital1 = Hospital A, and hospital2 = Hospital B. I want to combine these results together and add the referral.count properties together. Is there any way to do this with Cypher?

Was it helpful?

Solution

Not sure if I understood your domain correctly. My understanding is that you have a lot of paths (aka referrals) between two arbitrary hospitals and want to sum them up. In this case use

START hospital1 = Node:Hospitals("*:*")
MATCH (hospital1)-[:CHILD_DOCTOR]->(doctor1)-[referral:REFERRED]-(doctor2)<-[:CHILD_DOCTOR]-(hospital2)
WHERE hospital1 <> hospital2
RETURN hospital1, hospital2, sum(referral.count)

N.B. didn't try the query myself. For further discussion please create a sample dataset on http://console.neo4j.org.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top