I plan to migrate the data warehouse to SQL Server 2008 and try to find ways to replicate LAG, LEAD, FIRST_VALUE, and LAST_VALUE analytic functions from Oracle to SQL Server 2008. They are not included in SQL Server 2008, although the main mechanism for windowed analytic functions (for example, ROW_NUMBER, RANK and DENSE_RANK are present).
For these functions, you can achieve the same function by creating a subquery that assigns a row number to each row using ROW_NUMBER and then self-joins this query to find related rows with nearby row numbers (for LAG and LEAD), or row number 1 (for FIRST_VALUE )
I expect that performing self-connections will distract the operation's efficiency: but I don't yet have SQL Server to verify this. Therefore, without evaluating the performance, I wonder if there is a more efficient workaround that avoids self-connections.
By reviewing the documentation for user-defined aggregate functions , it is possible that the same code structure can be used to provide custom analytic functions.
So my question is: can you add an OVER () clause after a custom aggregate function to call it as an analytic function?
If so, is the Terminate () method called once per line? Is there anything special needed to get the strings sent to your UDF in the order specified in the OVER () clause?