title: PostgreSQL: UPDATE using aggregate function
body: <p>I want to update the table <code>paneldata</code> setting the column <code>ibase</code> using an aggregate function.</p>
  
  <pre><code>UPDATE paneldata p
  SET ibase=SUM(1/i.dist)
  FROM ibaselang i
  WHERE p.gid=i.gid
  AND i.instp&lt;p.period
  </code></pre>
  
  <p>This results in <code>ERROR:  aggregate functions are not allowed in UPDATE</code></p>
  
  <h2>TABLE DEFINITIONS</h2>
  
  <pre><code>CREATE TABLE public.ibaselang
  (
    gid integer,
    dist double precision,
    buildid integer,
    instp smallint
  )
  WITH (
    OIDS=FALSE
  );
  </code></pre>
  
  <h2>Solution Approach</h2>
  
  <p>Unfortunately I don't know how to implement my <code>WHERE</code> functions in a subquery.</p>