title: Recursive SQL statement (Postgresql) - simplified version
body: <p>This is simplified question for more complicated one posted here: </p>
  
  <p><a href="https://stackoverflow.com/questions/11558328/recursive-sql-statement-postgresql-9-1-4">Recursive SQL statement (PostgreSQL 9.1.4)</a></p>
  
  <p><strong>Simplified question</strong></p>
  
  <p>Given you have upper triangular matrix stored in 3 columns (RowIndex, ColumnIndex, MatrixValue):</p>
  
  <pre><code>   ColumnIndex       
      1   2   3   4   5
  1   2   2   3   3   4
  2   4   4   5   6   X
  3   3   2   2   X   X
  4   2   1   X   X   X
  5   1   X   X   X   X
  </code></pre>
  
  <p>X values are to be calculated using the following algorithm:</p>
  
  <pre><code>M[i,j] = (M[i-1,j]+M[i,j-1])/2
  (i= rows, j = columns, M=matrix)
  
  Example:
  M[3,4] = (M[2,4]+M[3,3])/2
  M[3,5] = (m[2,5]+M[3,4])/2
  </code></pre>
  
  <p>The full required result is:</p>
  
  <pre><code>   ColumnIndex       
      1   2   3    4      5
  1   2   2   3    3      4
  2   4   4   5    6      5
  3   3   2   2    4      4.5
  4   2   1   1.5  2.75   3.625
  5   1   1   1.25 2.00   2.8125
  </code></pre>
  
  <p>Sample data:</p>
  
  <pre><code>create table matrix_data (
      RowIndex integer,
      ColumnIndex integer,
      MatrixValue numeric);
  
      insert into matrix_data values (1,1,2);
      insert into matrix_data values (1,2,2);
      insert into matrix_data values (1,3,3);
      insert into matrix_data values (1,4,3);
      insert into matrix_data values (1,5,4);
      insert into matrix_data values (2,1,4);
      insert into matrix_data values (2,2,4);
      insert into matrix_data values (2,3,5);
      insert into matrix_data values (2,4,6);
      insert into matrix_data values (3,1,3);
      insert into matrix_data values (3,2,2);
      insert into matrix_data values (3,3,2);
      insert into matrix_data values (4,1,2);
      insert into matrix_data values (4,2,1);
      insert into matrix_data values (5,1,1);
  </code></pre>
  
  <p>Can this be done?</p>