title: Neo4j Cypher - How to Count Multiple Property Values With Cypher Efficiently And Paginate Properly
body: <p>I am struggling to get the proper cypher that is both efficient and allows pagination through skip and limit. </p>
  
  <p>Here is the simple scenario: I have the related nodes (company)&lt;-[A]-(set)&lt;-[B]-(job) where there are multiple instances of (set) with distinct (job) instances related to them.  The (job) nodes have a specific status property that can hold one of several states. We need to count the number of (job) nodes in a particular state per (set) and use skip and limit to paginate on the distinct (set) nodes.</p>
  
  <p>So we can get a very efficient query for job.status counts using this.</p>
  
  <pre><code>match (c:Company {id: 'MY.co'})&lt;-[:type_of]-(s:Set)&lt;-[:job_for]-(j:Job) 
  return s.Description, j.Status, count(*) as StatusCount;
  </code></pre>
  
  <p>Which will give us a rows of the Set.Description, Job.Status, and JobStatus count.  But we will get multiple rows for the Set based on the Job.Status.  This is not conducive to paging over distinct sets though.  Something like:</p>
  
  <pre><code>s.Description        j.Status       StatusCount
  -------------------+--------------+----------------
  Set 1              | Unassigned   | 10
  Set 1              | Completed    | 2
  Set 2              | Unassigned   | 3
  Set 1              | Reviewed     | 10
  Set 3              | Completed    | 4
  Set 2              | Reviewed     | 7
  </code></pre>
  
  <p>What we are trying to achieve with the proper cypher is result rows based on distinct Sets.  Something like this:</p>
  
  <pre><code>s.Description        Unassigned      Completed    Reviewed
  -------------------+--------------+-------------+----------
  Set 1              | 10           | 2           | 10
  Set 2              | 3            | 0           | 7
  Set 3              | 0            | 4           | 0
  </code></pre>
  
  <p>This would then allow us to paginate over Sets using skip and limit properly.</p>
  
  <p>I have tried many different approaches and cannot seem to find the right combination for this type of result.  Anyone have any ideas?  Thanks!</p>
  
  <p>** EDIT - Using the answer provided by MIchael, here's how to get the status count values in java **</p>
  
  <pre><code>match (c:Company {id: 'MY.co'})&lt;-[:type_of]-(s:Set)&lt;-[:job_for]-(j:Job) 
  with s, j.Status as Status,count(*) as StatusCount
  return s.Description, collect({Status:Status,StatusCount:StatusCount]) as StatusCounts;
  
  
     List&lt;Object&gt; statusMaps =  (List&lt;Object&gt;) row.get("StatusCounts");
     for(Object statusEntry : statusMaps ) {
       Map&lt;String,Object&gt; statusMap = (Map&lt;String,Object&gt;) statusEntry;
       String status = (String) statusMap.get("Status");
       Number count = statusMap.get("StatusCount");
      }
  </code></pre>