title: How to Update with subquery in PostgreSQL
body: <p>I have a function in MS SQL Server just like this:</p>
  <pre><code>UPDATE r
  SET
      monthly =
  (
      SELECT SUM(-h.value_ini - h.purchase + h.sold + h.value_fin)
      FROM   hist_portfolio AS h
      WHERE  h.comp_id = r.comp_id
       AND h.port_id = r.port_id 
       AND h.exte_id = r.cate_id
       AND h.type_id = @type_rel_aux
       AND h.hcar_day &gt; @date_month_before
       AND h.hcar_day &lt;= @date_base
  )
     FROM   #Month_Table r
     WHERE  type = 1;
  </code></pre>
  <p>and thats the result (after update):</p>
  <pre><code>    Seq     monthly 
     2        102471,34
     1        -5129,46
     3        -29841,23
     4        0
  </code></pre>
  <p>But when I execute the same update in a fuction in PostgreSQL, all the rows get the same value:</p>
  <pre><code>UPDATE Month_Table
      SET variacao_mes_rs = (
  SELECT SUM(-h.value_ini - h.purchase + h.sold + h.value_fin)
  FROM   hist_portfolio AS h
  WHERE  h.comp_id = r.comp_id
   AND h.port_id = r.port_id 
   AND h.exte_id = r.cate_id
   AND h.type_id = v_type_rel_aux
   AND h.hcar_day &gt; v_date_month_before
   AND h.hcar_day &lt;= v_date_base)     FROM   Month_Table r   WHERE  type = 1;
  </code></pre>
  <p>Result (after update), all the same value of  Seq 3:]</p>
  <pre><code>Seq     monthly 
  1   -29841,23
  2   -29841,23
  3   -29841,23
  4   -29841,23
  </code></pre>
  <p>I don't see the cause of the problem...
  Does PostgreSQL have different rules on UPDATE?
  Can anyone help me?</p>