title: replace NULL values with latest non-NULL value in resultset series (SQL Server 2008 R2)
body: <p>for SQL Server 2008 R2 </p>
  
  <p>I have a resultset that looks like this (note [price] is numeric, NULL below represents a 
  NULL value, the result set is ordered by product_id and timestamp)</p>
  
  <pre><code>product timestamp          price 
  ------- ----------------   -----
     5678 2008-01-01 12:00   12.34
     5678 2008-01-01 12:01    NULL
     5678 2008-01-01 12:02    NULL
     5678 2008-01-01 12:03   23.45
     5678 2008-01-01 12:04    NULL
  </code></pre>
  
  <p>I want to transform that to a result set that (essentially) copies a non-null value from the latest preceding row, to produce a resultset that looks like this:</p>
  
  <pre><code>product timestamp          price  
  ------- ----------------   -----
     5678 2008-01-01 12:00   12.34
     5678 2008-01-01 12:01   12.34
     5678 2008-01-01 12:02   12.34
     5678 2008-01-01 12:03   23.45
     5678 2008-01-01 12:04   23.45
  </code></pre>
  
  <p>I don't find any aggregate/windowing function that will allow me to do this (again this ONLY needed for SQL Server 2008 R2.)</p>
  
  <p>I was hoping to find an analytic aggregate function that do this for me, something like...</p>
  
  <pre><code>LAST_VALUE(price) OVER (PARTITION BY product_id ORDER BY timestamp)
  </code></pre>
  
  <p>But I don't seem to find any way to do a "cumulative latest non-null value" in the window (to bound the window to the preceding rows, rather than the entire partition)</p>
  
  <p>Aside from creating a table-valued user defined function, is there any builtin that would accomplish this?</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Apparently, this functionality is available in the 'Denali' CTP, but not in SQL Server 2008 R2.</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/hh231517%28v=SQL.110%29.aspx" rel="noreferrer">LAST_VALUE http://msdn.microsoft.com/en-us/library/hh231517%28v=SQL.110%29.aspx</a></p>
  
  <p>I just expected it to be available in SQL Server 2008. It's available in Oracle (since 10gR2 at least), and I can do something similar in MySQL 5.1, using a local variable.</p>
  
  <p><a href="http://download.oracle.com/docs/cd/E14072_01/server.112/e10592/functions083.htm" rel="noreferrer">http://download.oracle.com/docs/cd/E14072_01/server.112/e10592/functions083.htm</a></p>