title: Limit join to one row
body: <p>I have the following query:</p>
  
  <pre><code>SELECT sum((select count(*) as itemCount) * "SalesOrderItems"."price") as amount, 'rma' as     
      "creditType", "Clients"."company" as "client", "Clients".id as "ClientId", "Rmas".* 
  FROM "Rmas" JOIN "EsnsRmas" on("EsnsRmas"."RmaId" = "Rmas"."id") 
      JOIN "Esns" on ("Esns".id = "EsnsRmas"."EsnId") 
      JOIN "EsnsSalesOrderItems" on("EsnsSalesOrderItems"."EsnId" = "Esns"."id" ) 
      JOIN "SalesOrderItems" on("SalesOrderItems"."id" = "EsnsSalesOrderItems"."SalesOrderItemId") 
      JOIN "Clients" on("Clients"."id" = "Rmas"."ClientId" )
  WHERE "Rmas"."credited"=false AND "Rmas"."verifyStatus" IS NOT null 
  GROUP BY "Clients".id, "Rmas".id;
  </code></pre>
  
  <p>The problem is that the table <code>"EsnsSalesOrderItems"</code> can have the same <code>EsnId</code> in different entries. I want to restrict the query to only pull the last entry in <code>"EsnsSalesOrderItems"</code> that has the same <code>"EsnId"</code>. </p>
  
  <p>By "last" entry I mean the following:</p>
  
  <p>The one that appears last in the table <code>"EsnsSalesOrderItems"</code>. So for example if <code>"EsnsSalesOrderItems"</code> has two entries with <code>"EsnId" = 6</code> and <code>"createdAt" = '2012-06-19'</code> and <code>'2012-07-19'</code> respectively it should only give me the entry from <code>'2012-07-19'</code>.</p>