在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。