title: How to combine two rows in a table postgresql?
body: <p>Normaly I wouldn't ask this question however I have a bit of unique situation where I'm having troudle in How to do it.</p>
  
  <p>I have a table <code>A</code> as follows:</p>
  
  <pre><code>uniqueid , itemid, quantity, selected
  1            2455     10        TRUE
  2            7458     50        FALSE
  3            58494    20.6      FALSE
  4            2455     20        TRUE
  </code></pre>
  
  <p>I need to write a combine function that takes rows which are <code>TRUE</code> of the same part and combine them to one row (with the updated <code>quantity</code>).</p>
  
  <p>meaning I want to get:</p>
  
  <pre><code>uniqueid , itemid, quantity, selected
  1            2455     30        FASLE
  2            7458     50        FALSE
  3            58494    20.6      FALSE/TRUE (doesn't matter)
  </code></pre>
  
  <p>OR:</p>
  
  <pre><code>uniqueid , itemid, quantity, selected
  2            7458     50        FALSE
  3            58494    20.6      FALSE
  4            2455     30        FALSE/TRUE (doesn't matter)
  </code></pre>
  
  <p>My function doesn't get any paramenters... I need some method which I can identfy and work with the rows which are selected.
  First I thought of doing:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION func1()
    RETURNS void AS 
  $BODY$
  declare 
      ROW RECORD
  begin
  for ROW in select * from A where selected
      LOOP
          do combine code     
      end loop;
  end;                
  $BODY$
    LANGUAGE plpgsql VOLATILE
  </code></pre>
  
  <p>However this code will not work as after each combine two rows become one. In the above example the loop will generate two iterations, but I need only 1 iteration.. only 1 combine action neeeded.</p>
  
  <p>I only need assistance in how to get the structure of the function - loop? if? how to save the rows?. no need to write the combine code.</p>
  
  <p><strong>To make it simpler</strong> assume there can be only two rows which <code>selected</code> is <code>TRUE</code>.</p>
  
  <p><strong>Note</strong>: <code>func1</code> is supposed to <strong>save</strong> the new state into <code>A</code>.</p>