FIRST_VALUE (Lakehouse v1)
Devuelve el primer valor de un grupo ordenado de valores.
Consulte también:
Sintaxis de Analyze
Sección titulada «Sintaxis de Analyze»func.first_value(<expr>).over(partition_by=[<columns>], order_by=[<columns>])Ejemplos de Analyze
Sección titulada «Ejemplos de Analyze»table.employee_id, table.first_name, table.last_name, table.salary, func.first_value(table.first_name).over(order_by=table.salary.desc()).alias('highest_salary_first_name')
employee_id | first_name | last_name | salary | highest_salary_first_name------------+------------+-----------+---------+--------------------------4 | Mary | Williams | 7000.00 | Mary2 | Jane | Smith | 6000.00 | Mary3 | David | Johnson | 5500.00 | Mary1 | John | Doe | 5000.00 | Mary5 | Michael | Brown | 4500.00 | MarySintaxis de SQL
Sección titulada «Sintaxis de SQL»FIRST_VALUE(expression) OVER ([PARTITION BY partition_expression] ORDER BY order_expression [window_frame])Para conocer la sintaxis del marco de ventana, consulte Sintaxis del marco de ventana.
Ejemplos de SQL
Sección titulada «Ejemplos de SQL»CREATE TABLE employees ( employee_id INT, first_name VARCHAR(50), last_name VARCHAR(50), salary DECIMAL(10,2));
INSERT INTO employees (employee_id, first_name, last_name, salary)VALUES (1, 'John', 'Doe', 5000.00), (2, 'Jane', 'Smith', 6000.00), (3, 'David', 'Johnson', 5500.00), (4, 'Mary', 'Williams', 7000.00), (5, 'Michael', 'Brown', 4500.00);
-- Use FIRST_VALUE to retrieve the first name of the employee with the highest salarySELECT employee_id, first_name, last_name, salary, FIRST_VALUE(first_name) OVER (ORDER BY salary DESC) AS highest_salary_first_nameFROM employees;
employee_id | first_name | last_name | salary | highest_salary_first_name------------+------------+-----------+---------+--------------------------4 | Mary | Williams | 7000.00 | Mary2 | Jane | Smith | 6000.00 | Mary3 | David | Johnson | 5500.00 | Mary1 | John | Doe | 5000.00 | Mary5 | Michael | Brown | 4500.00 | Mary