title: Cypher: Limiting the number of returned links for a selection of nodes
body: <p>I am failing at an apparently simple query.</p>
  
  <p>Algo:</p>
  
  <ol>
  <li><p>Sort nodes by some value (desc):</p>
  
  <pre><code>MATCH (n) WHERE has(n.II_VAL) WITH n, n.II_VAL as ShInflInd order by ShInflInd desc
  </code></pre></li>
  <li><p>For each node, return its direct neighbors (ordered by a second value):</p>
  
  <pre><code>MATCH (n)-[r]-&gt;(m) with n, m, m.VALUE as SubsOpRev order by SubsOpRev desc
  </code></pre></li>
  </ol>
  
  <p>The challenge is to return the top 100 n nodes and for each of them only 10 relations r (if possible in a single row).</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Sorry for being so unspecific that people understood me wrong.</p>
  
  <p>In detail, the following query gives me the top nodes, sorted:</p>
  
  <pre><code>MATCH (n) WHERE HAS(n.II_VAL)
  WITH n, n.`II_VAL` AS ShInflInd ORDER BY ShInflInd DESC
  RETURN n.NAME LIMIT 100;
  
  +--------------------------------------+
  | n.NAME                               |
  +--------------------------------------+
  | "PUBLIC"                             |
  | "BARCLAYS PLC"                       |
  </code></pre>
  
  <p>Now I can add the sub-query to this which adds the links:</p>
  
  <pre><code>MATCH (n) WHERE HAS(n.II_VAL)
  WITH n, n.`II_VAL` AS ShInflInd ORDER BY ShInflInd DESC LIMIT 100
  MATCH (n)-[r]-&gt;(m) WHERE HAS(m.VALUE)
  WITH r, n, m, m.VALUE AS SubsOpRev
  RETURN n.NAME, r.WEIGHT_MERGED, m.NAME, SubsOpRev LIMIT 10;
  
  +----------------------------------------------------------------------------------------+
  | n.NAME   | r.WEIGHT_MERGED    | m.NAME                                     | SubsOpRev |
  +----------------------------------------------------------------------------------------+
  | "PUBLIC" | 0.66               | "VBS MUTUAL BANK"                          | 2630      |
  | "PUBLIC" | 0.2923             | "STRATCORP LIMITED"                        | 10842     |
  </code></pre>
  
  <p>Now what I would like, is that after returning 10 links for "PUBLIC" (possibly sorted by r.WEIGHT_MERGED or SubsOpRev) the query returns the second node ("BARCLAYS PLC") and its 10 links, etc.</p>
  
  <p>I have tried:</p>
  
  <pre><code>MATCH (n) WHERE HAS(n.II_VAL)
  WITH n, n.`II_VAL` AS ShInflInd ORDER BY ShInflInd DESC
  MATCH (n)-[r]-&gt;(m) WHERE HAS(m.VALUE)
  WITH r, n, m, m.VALUE AS SubsOpRev
  RETURN collect([n.NAME, r.WEIGHT_MERGED, m.NAME, SubsOpRev])[0..10];
  </code></pre>
  
  <p>Resulting in:</p>
  
  <pre><code>+------------------------------------------------------------------------------------------------------------------------------------------+
  | collect([n.NAME, r.WEIGHT_MERGED, m.NAME, SubsOpRev])[0..3]                                                                              |
  +------------------------------------------------------------------------------------------------------------------------------------------+
  | [["PUBLIC",0.66,"VBS MUTUAL BANK",2630],["PUBLIC",0.2923,"STRATCORP LIMITED",10842], ...
  </code></pre>
  
  <p>which means I am still stuck with "PUBLIC".</p>
  
  <p>Modifying the query a little makes things worse, as it returns totally different data:</p>
  
  <pre><code>MATCH (n) WHERE HAS(n.II_VAL)
  WITH n, n.`II_VAL` AS ShInflInd ORDER BY ShInflInd DESC
  MATCH (n)-[r]-&gt;(m) WHERE HAS(m.VALUE)
  WITH r, n, m, m.VALUE AS SubsOpRev
  RETURN n.NAME, collect([r.WEIGHT_MERGED, m.NAME, SubsOpRev])[0..10] LIMIT 3;
  
  +------------------------------------------------------------------------------+
  | n.NAME                | collect([r.WEIGHT_MERGED, m.NAME, SubsOpRev])[0..10] |
  +------------------------------------------------------------------------------+
  | "RS-INVEST AS"        | [[0.5,"VERUCO EIENDOM AS",100]]                      |
  | "DBM"                 | [[0.1435,"CHELYABINSKOBLGAZ",6752]]                  |
  </code></pre>
  
  <p>Ideally, the query should yield something like</p>
  
  <pre><code>| [["PUBLIC",0.66,"VBS MUTUAL BANK",2630],["PUBLIC",0.2923,"STRATCORP LIMITED",10842], ... |
  | [["BARCLAYS PLC",x,"XYZ",y], ... |
  </code></pre>