title: Neo4j 2.0 How do I count relationships in the CASE clause?
body: <p>I've been writing a cypher query to count all nodes with a label, property, or relationship that contain the criteria typed in by the user.  When I do this query, I want a count of all the nodes with unique label names (which I've accomplished).  <strong>I also want a count of all the nodes that contain specific relationships.</strong></p>
  
  <p>This is the part of the query that returns <strong>a count of all the nodes with the labels</strong>: Fruit, Lesson, Tech, and TestData.  I left out the WHERE clause because it's pretty long.</p>
  
  <pre><code>match (n)
  return 
  sum(CASE when any(l IN labels(n) WHERE l='Fruit') THEN 1 ELSE 0 END) AS Fruit,      
  sum(CASE when any(l IN labels(n) WHERE l='Lesson') THEN 1 ELSE 0 END) AS Lesson,
  sum(CASE when any(l IN labels(n) WHERE l='Tech') THEN 1 ELSE 0 END) AS Tech,
  sum(CASE when any(l IN labels(n) WHERE l='TestData') THEN 1 ELSE 0 END) AS TestData
  </code></pre>
  
  <p>It returns</p>
  
  <pre><code>Fruit       Lesson         Tech         TestData
  1000        20              100         50
  </code></pre>
  
  <p>However, I'd also like to count the number of nodes that had specific relationships (I know the names ahead of time) like "KNOWS", "IS_A", and "DESTINATION."  For instance, if a user was searching for the word "knows" and the resulting nodes had a relationship called "knows," then I would count that node.  Afterwards, my query would report that I had found 20 nodes that were connected via the "knows" relationship.</p>
  
  <p><strong>I'd like to do this without excluding any of my result nodes</strong>. Notice I didn't include any relationships in the match clause.  I still want to include nodes that don't have a relationship (I don't care about counting that).  </p>
  
  <p>Does anyone know how to do this?  Can it be done? </p>
  
  <p>I'm looking for something similar to this:</p>
  
  <pre><code>match (n)
  return
  sum(CASE when any(r IN rels(n) WHERE r='KNOWS') THEN 1 ELSE 0 END) AS KNOWS,      
  sum(CASE when any(r IN rels(n) WHERE r='IS_A') THEN 1 ELSE 0 END) AS IS_A,
  sum(CASE when any(r IN rels(n) WHERE r='DESTINATION') THEN 1 ELSE 0 END) AS DESTINATION,
  </code></pre>