Question on Window Function 2

Mikhail Firsov 1,876 Reputation points
2021-03-19T11:17:15.53+00:00

Hello,

Help me please with this:

79550-q-fv-lv.png

Thank you in advance,
Michael

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,554 questions
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 101.2K Reputation points MVP
    2021-03-19T23:20:15.917+00:00

    Yes, you get the last value so far when you only say ORDER BY col. When you leave out the window, this defaults to ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.

    Try this for a variation:

    SELECT name, column_id, last_value(name) OVER(ORDER BY column_id ROWS BETWEEN 3 PRECEDING AND 2 FOLLOWING )
    FROM   sys.all_columns
    WHERE  object_id = object_id('sys.objects')
    

    `

    1 person found this answer helpful.

6 additional answers

Sort by: Most helpful
  1. Mikhail Firsov 1,876 Reputation points
    2021-03-19T14:46:22.077+00:00

    Here's the test without ties:

    79695-q-fv-lv4.png

    0 comments No comments

  2. Mikhail Firsov 1,876 Reputation points
    2021-03-22T09:51:23.07+00:00

    Thank you all for your explanations!

    It works - just forgot that the default frame is ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW!

    select Number, Rank1,
    LAST_VALUE(Number) OVER (ORDER BY Rank1 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS [Max_Number]
    FROM Persentile2

    80193-q-fv-lv5.png

    Regards,
    Michael

    0 comments No comments