FIRST_VALUE

  • Comments posted to this topic are about the item FIRST_VALUE

  • Hi Steve,

    I got the below answers in my SQL 2014 instance.. Could you please explain why this answer was incorrect...

    Escobar2014-04-06 00:00:00

    Escobar2014-04-07 00:00:00

    Escobar2014-04-07 00:00:00

    Jeter2014-04-01 00:00:00

    Jeter2014-04-01 00:00:00

    Jeter2014-04-01 00:00:00

    Tulo2014-04-01 00:00:00

    Tulo2014-04-01 00:00:00

    Tulo2014-04-01 00:00:00

    ______________________________________________________________Every Problem has a Solution; Every Solution has a Problem: 🙂

  • Great question, thanks.

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

  • karthik babu (10/27/2014)


    Hi Steve,

    I got the below answers in my SQL 2014 instance.. Could you please explain why this answer was incorrect...

    Escobar2014-04-06 00:00:00

    Escobar2014-04-07 00:00:00

    Escobar2014-04-07 00:00:00

    Jeter2014-04-01 00:00:00

    Jeter2014-04-01 00:00:00

    Jeter2014-04-01 00:00:00

    Tulo2014-04-01 00:00:00

    Tulo2014-04-01 00:00:00

    Tulo2014-04-01 00:00:00

    I'm not sure what you did, but I get the correct results.

    CREATE TABLE #hits (team CHAR(3),player VARCHAR(15),hitdate DATE,hits TINYINT);

    INSERT INTO #hits

    VALUES ('NYY','Jeter' ,'2014-04-01',1)

    ,('NYY','Jeter' ,'2014-04-03',1)

    ,('NYY','Jeter' ,'2014-04-06',2)

    ,('COL','Tulo' ,'2014-04-01',1)

    ,('COL','Tulo' ,'2014-04-02',1)

    ,('COL','Tulo' ,'2014-04-03',2)

    ,('KC' ,'Escobar' ,'2014-04-06',1)

    ,('KC' ,'Escobar' ,'2014-04-07',1)

    ,('KC' ,'Escobar' ,'2014-04-08',2);

    SELECT

    player

    ,FIRST_VALUE(h.hitdate) OVER (PARTITION BY player ORDER BY hitdate)

    FROM #hits AS h;

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

  • Nice question Steve, checked on SQL SERVER 2012 working fine on that, do not know about 2014 thou.

  • Its my bad... I inserted a space in one of the column...

    Sorry guys..!! :satisfied:

    ______________________________________________________________Every Problem has a Solution; Every Solution has a Problem: 🙂

  • Nice one, excellent for Monday morning, thank you.

    Iulian

  • This was removed by the editor as SPAM

  • Informative question & explanation, thanx.

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • Thanks Steve. A good one to wake up the brain on a Monday morning. I learned something from it, so it's a good way to start the day.

  • Thanks for the question. I learned something new today.



    Everything is awesome!

  • Excellent question. Thanks, Steve.

  • Nice question. It had me stumped for a while since I was looking for an answer with 3 dates. It made me pay attention to the distinct values of the results.

    Aigle de Guerre!

  • Nice reminder. I had actually completely forgotten that this function exists. (Perhaps for good reasons?)

    If "syntax error, no such function exists" had been included in the answer options, I might have fallen for it.


    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/

  • I'm not familiar with FIRST_VALUE. But I guessed it meant the first value that you run across. Since the data was partitioned by planer and ordered by date (for that player's dates) it seemed reasonable that it would display the first date for each player.

Viewing 15 posts - 1 through 15 (of 20 total)

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