title: How to query? (sql / jpql)
body: <p>I need to create a query, but a it difficult one (at least or me).</p>
  
  <p>I have Products stored in the db, each one belongs to a Factory.<br>
  Each Factory can work only one of its own Product at the same time.<br>
  Each Product have a number represents its order in the Factory's queue.<br>
  Each product has a field what is a number if its Factory has started working on it, or null if the Product is in the queue.</p>
  
  <p><strong>The query must return a list of Products, with the first Product of each Factory's queue, but only if the Factory is not working on any product currently.</strong></p>
  
  <p>See the following example:</p>
  
  <p><strong>Given:</strong></p>
  
  <pre><code>Id          FactoryId   Working     Order
  Product1    Factory1    4654        1
  
  Product2    Factory2    9841        3
  Product3    Factory2    NULL        6
  
  Product4    Factory3    NULL        4
  Product5    Factory3    NULL        7
  
  Product6    Factory4    NULL        2
  Product7    Factory4    3211        9
  
  product8    Factory5    NULL        10
  product9    Factory5    NULL        5
  product10   Factory5    NULL        13
  </code></pre>
  
  <p><strong>Result:</strong></p>
  
  <pre><code>Id          FactoryId   Working     Order
  Product4    Factory3    NULL        4
  product9    Factory5    NULL        5
  </code></pre>
  
  <p><strong>I use Spring Data JpaRepository.</strong></p>