NTH_VALUE (Lakehouse v1)
Gibt den N-ten Wert aus einer geordneten Gruppe von Werten zurück.
Siehe auch:
Analyze-Syntax
Abschnitt betitelt „Analyze-Syntax“func.nth_value(<expr>, <n>).over(partition_by=[<columns>], order_by=[<columns>])Analyze-Beispiele
Abschnitt betitelt „Analyze-Beispiele“table.employee_id, table.first_name, table.last_name, table.salary, func.nth_value(table.first_name, 2).over(order_by=table.salary.desc()).alias('second_highest_salary_first_name')
employee_id | first_name | last_name | salary | second_highest_salary_first_name------------+------------+-----------+---------+----------------------------------4 | Mary | Williams | 7000.00 | Jane2 | Jane | Smith | 6000.00 | Jane3 | David | Johnson | 5500.00 | Jane1 | John | Doe | 5000.00 | Jane5 | Michael | Brown | 4500.00 | JaneSQL-Syntax
Abschnitt betitelt „SQL-Syntax“NTH_VALUE(expression, n) OVER ([PARTITION BY partition_expression] ORDER BY order_expression [window_frame])Die Syntax des Fensterrahmens finden Sie unter Syntax des Fensterrahmens.
SQL-Beispiele
Abschnitt betitelt „SQL-Beispiele“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 NTH_VALUE to retrieve the first name of the employee with the second highest salarySELECT employee_id, first_name, last_name, salary, NTH_VALUE(first_name, 2) OVER (ORDER BY salary DESC) AS second_highest_salary_first_nameFROM employees;
employee_id | first_name | last_name | salary | second_highest_salary_first_name------------+------------+-----------+---------+----------------------------------4 | Mary | Williams | 7000.00 | Jane2 | Jane | Smith | 6000.00 | Jane3 | David | Johnson | 5500.00 | Jane1 | John | Doe | 5000.00 | Jane5 | Michael | Brown | 4500.00 | Jane