'Neo4j / Cypher: Matching "Triangles"

I am new to Neo4j and Cypher and I can't get my head around this problem. Let's suppose I have a very large graph with 3 different node types: Doctor, Diagnosis and Product. I would like to do the following thing:

For each Doctor-Diagnosis connected pair, count how many different products are linked to both the doctor and the diagnosis.

A sample of my data would be:

Doctor_ID   Diagnosis   Product
   1           A          123
   1           B          527
   1           A          198
   2           K          471
   2           A          123
   3           J          024
   3           A          198
   3           A          123
   3           A          722

And I would like to get:

Doctor_ID    Diagnosis   COUNT 
   1            A          2
   1            B          1
   2            K          1
   2            A          1
   3            J          1
   3            A          3

Any help would be highly appreciated. Thanks!



Solution 1:[1]

Without any additional information about the schema, I would suggest the following query:

MATCH (d:Doctor)--(d1:Diagnosis)
MATCH (d)--(:Product)--(d1)
RETURN count(*) AS commonProducts

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tomaž Bratanič