CREATE TABLE overtime_table ( employee_name VARCHAR(50) NOT NULL, department VARCHAR(50) NOT NULL, hours INT NOT NULL, PRIMARY KEY (employee_name , department) );分部门查询加班时长最长的employee_name,期望结果如下:
+-------------------+------------+-------+-----------------+ | employee_name | department | hours |longest_time_name| +-------------------+------------+-------+-----------------+ | Mary Patterson | Accounting | 74 | Mary Patterson | | Jeff Firrelli | Accounting | 40 | Mary Patterson | | Diane Murphy | Accounting | 37 | Mary Patterson | | Anthony Bow | Finance | 66 | Anthony Bow | | William Patterson | Finance | 58 | Anthony Bow | | Gerard Bondur | Finance | 47 | Anthony Bow | +-------------------+------------+-------+-----------------+
SELECT
employee_name,
department,
hours,
LAST_VALUE(employee_name) OVER (
PARTITION BY department
ORDER BY hours
) longest_time_name
FROM
overtime_table;
SELECT
employee_name,
department,
hours,
LAST_VALUE(employee_name) OVER (
PARTITION BY department
ORDER BY hours DESC
) longest_time_name
FROM
overtime_table;
SELECT
employee_name,
department,
hours,
FIRST_VALUE(employee_name) OVER (
PARTITION BY department
ORDER BY hours DESC
) longest_time_name
FROM
overtime_table;
其他选项均错误