title: How to merge two queries in Doctrine ORM
body: <p>I have table &quot;weights&quot;, where I have numbers of weights for various cameras.
  I want to sum last numbers for this cameras, therefore I am using two queries.
  First query selects max ids for every camera and second selects numbers of weight for these ids.</p>
  <pre><code>public function maxIds($cameras)
   {
       return $this-&gt;createQueryBuilder(&quot;c&quot;)
           -&gt;select('MAX(c.id) ')
           -&gt;where('c.camera IN (:value)')
           -&gt;setParameter('value', $cameras)
           -&gt;groupBy('c.camera')
           -&gt;getQuery()
           -&gt;getArrayResult();
   }
  
  public function totalWeight($ids)
  {
      return $this-&gt;createQueryBuilder(&quot;c&quot;)
          -&gt;select('SUM(c.number)')
          -&gt;where('c.id IN (:value)')
          -&gt;setParameter('value', $ids)
          -&gt;getQuery()
          -&gt;getOneOrNullResult();
  }
  </code></pre>
  <p>These two queries work fine, but I would like to combine them into one query.
  I am trying with this:</p>
  <pre><code>public function testWeight($cameras)
  {
      $qr = $this-&gt;createQueryBuilder('c')
          -&gt;select('MAX(c.id) ')
          -&gt;where('c.camera IN (:value)')
          -&gt;setParameter('value', $cameras)
          -&gt;groupBy('c.camera');
  
      $qr-&gt;andWhere($qr-&gt;expr()-&gt;in('c.id',
          $this-&gt;_em-&gt;createQueryBuilder()
               -&gt;select('SUM(c.number)')
               -&gt;getDQL()
      ));
  
      return  $qr-&gt;getQuery()
                 -&gt;getOneOrNullResult();
  }
  </code></pre>
  <p>But I cannot success.
  How can I solve this problem?</p>