What are the advantages of using the new LAG and LEAD functions in SQL Server 2012?
Is it simply a matter of easier to write ans simpler to debug queries or is there also a performance improvement?
This is important for me, since we require this type of functionality very often and I need to know if we should recommend an upgrade in the near future.
If it’s only easier queries, it wont be worth the hassle (and costs) of upgrading.
To demonstrate a difference in the execution plan, I’ve used the winning solution from Dave’s SQL Authority blog:
Next to being way more elegant, it consumes much less resources.
Here’s the comparison of graphical execution plans:
Execution plans show one clear winner in this specific case.
Dave’s page has many possible different ways to get LEAD/LAG functionality. Maybe some of them would beat the SQL Server’s internal solution. Or, maybe not.