title: Jooq ERROR: missing FROM-clause entry for table for nested query(sum and group by)
body: <p>I have the following jooq statement executed on Postgres 9.6 :</p>
  
  <pre class="lang-java prettyprint-override"><code>DSLContext context = DSL.using(connection, POSTGRES_10);
  testSafesFundsAllocationRecord record = 
  context.select()
         .from(
            select(
              TEST_SAFES_FUNDS_ALLOCATION.LEDGER_ID,       
              sum(TEST_SAFES_FUNDS_ALLOCATION.AMOUNT)
                 .as(TEST_SAFES_FUNDS_ALLOCATION.AMOUNT))
           .from(TEST_SAFES_FUNDS_ALLOCATION)
           .groupBy(TEST_SAFES_FUNDS_ALLOCATION.LEDGER_ID))
         .where(TEST_SAFES_FUNDS_ALLOCATION.AMOUNT.greaterOrEqual(amount))
         .and(TEST_SAFES_FUNDS_ALLOCATION.LEDGER_ID.notIn(excludedLedgers))
         .orderBy(TEST_SAFES_FUNDS_ALLOCATION.AMOUNT.asc())
         .limit(1)
         .fetchOne()
         .into(TEST_SAFES_FUNDS_ALLOCATION);
  </code></pre>
  
  <p>It is translated to the following:</p>
  
  <pre><code>SQL [
  select "alias_88420990"."ledger_id", "alias_88420990"."amount" from (
  select "public"."test_safes_funds_allocation"."ledger_id", 
  sum("public"."test_safes_funds_allocation"."amount") as "amount" from "public"."test_safes_funds_allocation" group by "public"."test_safes_funds_allocation"."ledger_id") as "alias_88420990"
   where ("public"."test_safes_funds_allocation"."amount" &gt;= ? and 1 = 1) order by "public"."test_safes_funds_allocation"."amount" asc limit ?]; 
  </code></pre>
  
  <p>excludedLedgers -> is an empty string array</p>
  
  <p>and the result is:</p>
  
  <pre><code>org.postgresql.util.PSQLException: ERROR: missing FROM-clause entry for table "test_safes_funds_allocation"
    Position: 334
  </code></pre>
  
  <p>Can someone tell me what is the problem in the query ? it is nested one as you can see... but I can't seem to understand the problem.
  The Query does the following: 
  sum all the amount rows of the same ledger id(group by) and then from the output return the minimal row that has amount greater than variable amount , we can exclude specific <code>ledger_id</code> via the array(it is empty in this example).</p>
  
  <p>any help will be greatly appreciated,</p>
  
  <p>Regards</p>