title: Oracle Lag function with dynamic parameter
body: <p>I have a specific problem. I have a table which contains invalid values.
  I need to replace the invalid values (here <code>0</code>) with the previous value which  is bigger than <code>0</code>.</p>
  
  <p>The difficulty is, it is not appropiate for me to use an Update or an insert(Cursor and update would do it).Well my only way is to use a Select statement.</p>
  
  <p>When I use the <code>lag(col1, 1)</code> - function with when case, I get only one column with the correct value.</p>
  
  <pre><code>select col1, col2 realcol2,  
  (case 
    when col2 = 0 then 
      lag(col2,1,1) over (partition by col1 order by col1 )
    else
     col2
    end ) col2,     
  col3 realcol3,
  (case 
    when col3 = 0 then 
      lag(col3,1,1) over (partition by col1 order by col1 )
    else
     col3
    end ) col3
  from test_table 
  </code></pre>
  
  <p>Contents of <code>TEST_TABLE</code>:</p>
  
  <pre><code>---------------------------
   Col1 | Col2 | Col3 | Col4
  ---------------------------
    A   |  0   |  1   |  5
    B   |  0   |  4   |  0
    C   |  2   |  0   |  0
    D   |  0   |  0   |  0
    E   |  3   |  5   |  0
    F   |  0   |  3   |  0
    G   |  0   |  3   |  1
    A   |  0   |  1   |  5
    E   |  3   |  5   |  0
  </code></pre>
  
  <p>Expected query result:</p>
  
  <pre><code>---------------------------
   Col1 | Col2 | Col3 | Col4
  ---------------------------
    A   |  0   |  1   |  5
    B   |  0   |  4   |  5
    C   |  2   |  4   |  5
    D   |  2   |  4   |  5
    E   |  3   |  5   |  5
    F   |  3   |  3   |  5
    G   |  3   |  3   |  1
    A   |  3   |  1   |  5
    E   |  3   |  5   |  5
  </code></pre>