for SQL Server 2008 R2
I have a result set that looks like this (note [price] is numeric, NULL below is NULL, the result set is ordered using product_id and timestamp)
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
I want to convert this to a result set that (essentially) copies a nonzero value from the last previous row to create a result set that looks like this:
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
I find no aggregation / window function that will allow me to do this (again, this is ONLY necessary for SQL Server 2008 R2.)
I was hoping to find an analytic aggregate function that does this for me, something like ...
LAST_VALUE(price) OVER (PARTITION BY product_id ORDER BY timestamp)
But I didn't seem to find a way to make the "cumulative last non-zero value" in the window (bind the window to previous lines, not to the entire section)
Besides creating a user-defined table function, is there a built-in function that would do this?
UPDATE:
This functionality appears to be available in CTP 'Denali', but not in SQL Server 2008 R2.
LAST_VALUE http://msdn.microsoft.com/en-us/library/hh231517%28v=SQL.110%29.aspx
I just expected it to be available in SQL Server 2008. It is available in Oracle (at least with 10gR2), and I can do something similar in MySQL 5.1 using a local variable.
http://download.oracle.com/docs/cd/E14072_01/server.112/e10592/functions083.htm