title: How to group matched results in Neo4J/Cypher
body: <p>I'm very new to Neo4J and Cypher and I have to work on a highly relational dataset. The graph model looks like this</p>
  
  <p><a href="https://i.stack.imgur.com/g8Pdg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/g8Pdg.png" alt="enter image description here"></a></p>
  
  <p>Where in my data I have multiple instances of C,A,Le, and only a few instances of Lo, R, F.</p>
  
  <p>I am looking to find a way to get the following: For each unique pair of Lo-F, get all the C and count how many unique C for each such pair.</p>
  
  <p>In other words, I want all the groups of <code>C</code> that share the same <code>LO</code> and <code>F</code>, and I need my results to tell me:</p>
  
  <ul>
  <li>How many unique <code>C</code></li>
  <li>Common LO</li>
  <li>Common F</li>
  </ul>
  
  <p>My humble start was to <code>MATCH</code> a pattern like this (directions of arrows are mostly irrelevant for this problem):</p>
  
  <pre><code>MATCH (c:C)--(:A)--(le:LE)--(:R)--(f:F),
        (le)--(lo:LO)
  RETURN f, lo, c LIMIT 5
  </code></pre>
  
  <p>Which returns all matched patterns. I then proceeded to attempt to use <code>collect</code> and <code>count</code>. For example,  </p>
  
  <pre><code>MATCH (c:C)--(:A)--(le:LE)--(:R)--(f:F),
        (le)--(lo:LO)
  RETURN f, lo, collect(c), COUNT(*) as _counts
  ORDER BY _counts LIMIT 5
  </code></pre>
  
  <p>which seems to be giving me a table with unique f-lo pairs and the list of <code>C</code> plus the counts. However, the <code>collect(c)</code> column have repeated <code>C</code>, and as such the <code>_counts</code> are not correct. I then attempted to introduce <code>DISTINCT</code>:</p>
  
  <pre><code>MATCH (c:C)--(:A)--(le:LE)--(:R)--(f:F),
        (le)--(lo:LO)
  RETURN f, lo, collect(DISTINCT c), COUNT(*) as _counts
  ORDER BY _counts LIMIT 5
  </code></pre>
  
  <p>But I have the same results...</p>
  
  <p>I have googled extensively, but since I am also fairly new to SQL I find most of the tutorials and examples difficult to follow in their SQL-analogy.</p>
  
  <p>Also, would this be better framed as a clustering algorithm? I have installed the <code>algo</code> plugin but I have struggled to understand how to prepare the match for the <code>.stream</code> method in order to accomplish this.</p>