RANK (Lakehouse v1)
A função RANK() atribui uma classificação a cada valor dentro de um grupo ordenado de valores.
O valor da classificação começa em 1 e continua em sequência. Se dois valores forem iguais, eles têm a mesma classificação.
Sintaxe do Analyze
Seção intitulada “Sintaxe do Analyze”func.rank().over(partition_by=[<columns>], order_by=[<columns>])Exemplos do Analyze
Seção intitulada “Exemplos do Analyze”table.employee_id, table.first_name, table.last_name, table.department, table.salary, func.rank().over(order_by=table.salary).alias('rank')
| employee_id | first_name | last_name | department | salary | rank ||-------------|------------|-----------|------------|--------|------|| 1 | John | Doe | IT | 90000 | 1 || 2 | Jane | Smith | HR | 85000 | 2 || 3 | Mike | Johnson | IT | 82000 | 3 || 4 | Sara | Williams | Sales | 77000 | 4 || 5 | Tom | Brown | HR | 75000 | 5 |Sintaxe SQL
Seção intitulada “Sintaxe SQL”RANK() OVER ( [ PARTITION BY <expr1> ] ORDER BY <expr2> [ { ASC | DESC } ] [ <window_frame> ])Exemplos de SQL
Seção intitulada “Exemplos de SQL”Criar a tabela
CREATE TABLE employees ( employee_id INT, first_name VARCHAR, last_name VARCHAR, department VARCHAR, salary INT);Inserir dados
INSERT INTO employees (employee_id, first_name, last_name, department, salary) VALUES (1, 'John', 'Doe', 'IT', 90000), (2, 'Jane', 'Smith', 'HR', 85000), (3, 'Mike', 'Johnson', 'IT', 82000), (4, 'Sara', 'Williams', 'Sales', 77000), (5, 'Tom', 'Brown', 'HR', 75000);Classificar funcionários por salário
SELECT employee_id, first_name, last_name, department, salary, RANK() OVER (ORDER BY salary DESC) AS rankFROM employees;Resultado:
| employee_id | first_name | last_name | department | salary | rank |
|---|---|---|---|---|---|
| 1 | John | Doe | IT | 90000 | 1 |
| 2 | Jane | Smith | HR | 85000 | 2 |
| 3 | Mike | Johnson | IT | 82000 | 3 |
| 4 | Sara | Williams | Sales | 77000 | 4 |
| 5 | Tom | Brown | HR | 75000 | 5 |