在Oracle中,您可以使用MAX函数与GROUP BY子句结合进行分组统计
首先,我们创建一个名为employees的表:
CREATE TABLE employees (
id NUMBER PRIMARY KEY,
name VARCHAR2(50),
department_id NUMBER,
salary NUMBER
);
接下来,我们插入一些示例数据:
INSERT INTO employees (id, name, department_id, salary) VALUES (1, 'Alice', 10, 5000);
INSERT INTO employees (id, name, department_id, salary) VALUES (2, 'Bob', 10, 6000);
INSERT INTO employees (id, name, department_id, salary) VALUES (3, 'Charlie', 20, 7000);
INSERT INTO employees (id, name, department_id, salary) VALUES (4, 'David', 20, 8000);
现在,我们将使用MAX函数和GROUP BY子句按部门对员工的最高薪水进行分组统计:
SELECT department_id, MAX(salary) as max_salary
FROM employees
GROUP BY department_id;
此查询将返回以下结果:
DEPARTMENT_ID MAX_SALARY
------------- ----------
10 6000
20 8000
这表示部门10的最高薪水是6000,部门20的最高薪水是8000。