LEAD parameters

  • Comments posted to this topic are about the item LEAD parameters

  • Thanks for this interesting question.

    I am not usually using ranking functions , it is why I attended a session about them during the last SQL Server Days and the speaker gave 3 examples about LEAD which he explained during 15 minutes. So I was able to find the good answer ( I remember that he explained that usually the last parameter is related to the default value . I tried this choice , and that was correct ... ).

  • Thanks steve for the questions on LEAD and LAG

  • Thanks. As I'm still on 2008, I don't get to play with LEAD and LAG yet. Looking forward to having one soon, but the hardware hasn't arrived yet.

  • Seems like it's sort of ISNULL function. If it's encountered NULL, the 3rd parameter will replace the entered value. Nice Question.

    Thanks.

  • SQL-DBA-01 (5/15/2015)


    Seems like it's sort of ISNULL function. If it's encountered NULL, the 3rd parameter will replace the entered value. Nice Question.

    No, the default parameter of LAG and LEAD is only used when applying the specified offset results in a row outside of the range. If there is an actual row there, the value from that row will always be used, even when it is NULL.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Nice question. Raises an interesting issue:

    Hugo Kornelis (5/15/2015)


    SQL-DBA-01 (5/15/2015)


    Seems like it's sort of ISNULL function. If it's encountered NULL, the 3rd parameter will replace the entered value. Nice Question.

    No, the default parameter of LAG and LEAD is only used when applying the specified offset results in a row outside of the range. If there is an actual row there, the value from that row will always be used, even when it is NULL.

    Well, BOL explicitly states that the default replaces a NULL in the row found at the offset, but it's trivial to write code which verifies Hugo's statement (actually I only verified it for lag, but ...), so here we have another error in BOL and the BOL error is repeated in this QOTD. But teh BOL page also contains the ludicrous statement "NULL is returned if scalar_expression is nullable or default is set to NULL" :w00t: which suggests that NULL will be returned everywhere a a there is a row at offset if the relevant column is nullable.

    Tom

  • patricklambin (5/15/2015)


    Thanks for this interesting question.

    I am not usually using ranking functions , it is why I attended a session about them during the last SQL Server Days and the speaker gave 3 examples about LEAD which he explained during 15 minutes. So I was able to find the good answer ( I remember that he explained that usually the last parameter is related to the default value . I tried this choice , and that was correct ... ).

    +1

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • Ed Wagner (5/15/2015)


    Thanks. As I'm still on 2008, I don't get to play with LEAD and LAG yet. Looking forward to having one soon, but the hardware hasn't arrived yet.

    I'm in the same boat as you.

  • Iwas Bornready (5/18/2015)


    Ed Wagner (5/15/2015)


    Thanks. As I'm still on 2008, I don't get to play with LEAD and LAG yet. Looking forward to having one soon, but the hardware hasn't arrived yet.

    I'm in the same boat as you.

    I'm looking forward to LEAD and LAG. There's a number of performance comparisons I'd like to do against techniques I use now.

  • Easy one, thanks.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Hugo Kornelis (5/15/2015)


    SQL-DBA-01 (5/15/2015)


    Seems like it's sort of ISNULL function. If it's encountered NULL, the 3rd parameter will replace the entered value. Nice Question.

    No, the default parameter of LAG and LEAD is only used when applying the specified offset results in a row outside of the range. If there is an actual row there, the value from that row will always be used, even when it is NULL.

    That's good to know. I think I have to put ISNULL in some of my scripts now 🙂

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Nice question, learned something new.

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply