Skip to content

Latest commit

 

History

History
36 lines (26 loc) · 1.42 KB

first_value.md

File metadata and controls

36 lines (26 loc) · 1.42 KB
description
This section contains reference documentation for the FIRST_VALUE window function.

FIRST_VALUE

The FIRST_VALUE function returns the value from the first row in a window.

Signature

FIRST_VALUE(expression) [IGNORE NULLS | RESPECT NULLS] OVER ()

The default behavior is RESPECT NULLS. If the IGNORE NULLS option is specified, the function returns the first non-null row in each window (assuming one exists, null otherwise).

Example

This example computes the number of games played by a player in their first year.

SELECT playerName,
  yearID,
  numberOfGames,
  FIRST_VALUE(numberOfGames) OVER (
    PARTITION BY playerName
    ORDER BY yearID ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
  )
FROM baseballStats;

Output:

playerNameyearIDnumberOfGamesEXPR$3
p120001010
p120011510
p120021210
p21990120120
p21991124120
p320063030
p320072530
p320092030
p320101530