title: postgresql plpsql function with IF ELSE statement
body: <p>Suppose i have a table named t which is stored in a postgresql database.
  I have 6 columns named a, b, c, d, e, f.
  Columns a, b and c take values between 0 and 100, but on an aritmetic scale 0 &lt; a &lt; b &lt; c &lt; 100. Columns d, e and f take any value in the range 1 to 10.</p>
  
  <p>I want to compute the weighted average of columns d, e and f, but with a condition related to columns a, b and c. The condition is that the average will be computed only on the a, b and c columns that have values smaller than 50.</p>
  
  <p>I think this need a function so I started to do it:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION example(t character varying, a character varying, b character varying, c character varying, d character varying, e character varying, f character varying, g character varying) RETURNS double precision AS $$
  
  BEGIN
      ALTER TABLE t ADD COLUMN g double precision;
          UPDATE t
              IF a &gt; 50 THEN
                  SET g = d;
              ELSE 
                  IF b &gt; 50 THEN;
                      SET g = (d+e)/2;
                  END IF c &gt; 50 THEN
                          SET g = (d+e+f)/3;
              END IF;
  END;
  $$ LANGUAGE plpgsql;
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>ERROR:  syntax error at or near "$1"
  LINE 1: ALTER TABLE  $1  ADD COLUMN  $2  double precision
                   ^
  QUERY:  ALTER TABLE  $1  ADD COLUMN  $2  double precision
  CONTEXT:  SQL statement in PL/PgSQL function "example" near line 2
  
  ********** Error **********
  
  ERROR: syntax error at or near "$1"
  SQL state: 42601
  Context: SQL statement in PL/PgSQL function "example" near line 2
  </code></pre>
  
  <p>Can someone tell me were I am wrong so I can go ahead with computing the needed average?</p>