title: Postgres JSON_AGG and Order By not working together
body: <p>I have a query that looks like this:</p>
  
  <pre><code>SELECT *,
  (SELECT json_agg(deals.*) FROM deals WHERE vendors.id = deals.vendorid) as deals
  FROM vendors
  </code></pre>
  
  <p>That query works great, however when I try and order by a field in the <code>deals</code> table like this:</p>
  
  <pre><code>SELECT *,
  (SELECT json_agg(deals.*) FROM deals WHERE vendors.id = deals.vendorid ORDER BY priority) as deals
  FROM vendors
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>error: column "deals.priority" must appear in the GROUP BY clause or be used in an aggregate function
  </code></pre>
  
  <p>Any ideas how to do a sort like this?</p>