Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8851503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:10:16+00:00 2026-06-14T13:10:16+00:00

What are the advantages of using the new LAG and LEAD functions in SQL

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:10:18+00:00Added an answer on June 14, 2026 at 1:10 pm

    To demonstrate a difference in the execution plan, I’ve used the winning solution from Dave’s SQL Authority blog:

    • Solution to Puzzle – Simulate LEAD() and LAG() without Using SQL Server 2012 Analytic Function
    ;WITH T1
    AS (SELECT row_number() OVER (ORDER BY SalesOrderDetailID) N
             , s.SalesOrderID
             , s.SalesOrderDetailID
        FROM
            TempDB.dbo.LAG s
        WHERE
            SalesOrderID IN (20120303, 20120515, 20120824, 20121031))
    SELECT SalesOrderID
         , SalesOrderDetailID AS CurrentSalesOrderDetailID
    /*   , CASE
               WHEN N % 2 = 1 THEN
                   max(CASE
                       WHEN N % 2 = 0 THEN
                           SalesOrderDetailID
                   END) OVER (PARTITION BY (N + 1) / 2)
               ELSE
                   max(CASE
                       WHEN N % 2 = 1 THEN
                           SalesOrderDetailID
                   END) OVER (PARTITION BY N / 2)
           END LeadVal */
         , CASE
               WHEN N % 2 = 1 THEN
                   max(CASE
                       WHEN N % 2 = 0 THEN
                           SalesOrderDetailID
                   END) OVER (PARTITION BY N / 2)
               ELSE
                   max(CASE
                       WHEN N % 2 = 1 THEN
                           SalesOrderDetailID
                   END) OVER (PARTITION BY (N + 1) / 2)
           END PreviousSalesOrderDetailID
    FROM
        T1
    ORDER BY
        SalesOrderID
      , SalesOrderDetailID;
    
    
    
    SELECT SalesOrderID
         , SalesOrderDetailID AS CurrentSalesOrderDetailID
         , LAG(SalesOrderDetailID, 1, 0) OVER (ORDER BY SalesOrderID, SalesOrderDetailID) AS PreviousSalesOrderDetailID
    FROM TempDB.dbo.LAG
    WHERE SalesOrderID  IN (20120303, 20120515, 20120824, 20121031);
    
    
    
    
    
    Warning: Null value is eliminated by an aggregate or other SET operation.
    
    (10204 row(s) affected)
    Table 'Worktable'. Scan count 6, logical reads 81638, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    Table 'LAG'. Scan count 4, logical reads 48, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    
     SQL Server Execution Times:   CPU time = 297 ms,  elapsed time = 332 ms.
    
    --- versus ---
    
    (10204 row(s) affected)
    Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    Table 'LAG'. Scan count 4, logical reads 48, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    
     SQL Server Execution Times:   CPU time = 78 ms,  elapsed time = 113 ms.
    

    Next to being way more elegant, it consumes much less resources.

    Here’s the comparison of graphical execution plans:

    Execution Plans Show Clear Winner in This Specific Case

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What are advantages of using a one-to-one table relationship as opposed to simply storing
Could some one please explain the advantages and disadvantages of using the new MVC2
Can someone explain to me the advantages of using an IOC container over simply
I want to take advantage of the new features in Windows 7 using C#
What are the advantages of using Asynchronous controllers in my ASP.NET MVC website? Please
What are the advantages of using listview over gridview? I need pagination, editing rows,
Are there any advantages of using one over the other besides readability?
What are the advantages of using Framework over Library or other way around while
What are the advantages of using var foo = []; over using var bar
What would be some advantages of using etags/stale?/fresh_when? instead of page caching (on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.