Outdated Version

You are viewing an older version of this section. View current production version.

FIRST_VALUE

Value function. Returns an expression evaluated at the first row of the window frame. If there are no rows in the frame, returns NULL. For an overview of window functions in MemSQL, see Window Functions Guide.

Syntax

FIRST_VALUE ( expression ) OVER (
    [PARTITION BY (col | expr), ...] 
    [ORDER BY (col | expr), ...]
    [frame_clause]
)

Arguments

  • expression: any expression. This may be a column name, the result of another function, or a math operation.

Return Type

The result type of expression

Examples

memsql> create table example (a int, b int);

memsql> insert into example values (1, 2), (2, 2), (3, 3), (4, 3);

memsql> select a, first_value(a) over(order by (a)) from example;
+------+-----------------------------------+
| a    | first_value(a) over(order by (a)) |
+------+-----------------------------------+
|    1 |                                 1 |
|    2 |                                 1 |
|    3 |                                 1 |
|    4 |                                 1 |
+------+-----------------------------------+

memsql> select a, first_value(a) over(order by (a) rows between current row and unbounded following) from example;
+------+------------------------------------------------------------------------------------+
| a    | first_value(a) over(order by (a) rows between current row and unbounded following) |
+------+------------------------------------------------------------------------------------+
|    1 |                                                                                  1 |
|    2 |                                                                                  2 |
|    3 |                                                                                  3 |
|    4 |                                                                                  4 |
+------+------------------------------------------------------------------------------------+