在Oracle中,可以使用JOIN语句将多个表连接起来,并对结果集进行数据统计
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50),
department_id INT
);
CREATE TABLE departments (
id INT PRIMARY KEY,
name VARCHAR(50)
);
INSERT INTO employees (id, name, department_id) VALUES (1, 'Alice', 101);
INSERT INTO employees (id, name, department_id) VALUES (2, 'Bob', 102);
INSERT INTO employees (id, name, department_id) VALUES (3, 'Charlie', 101);
INSERT INTO departments (id, name) VALUES (101, 'HR');
INSERT INTO departments (id, name) VALUES (102, 'IT');
SELECT d.name AS department_name, COUNT(e.id) AS employee_count
FROM employees e
JOIN departments d ON e.department_id = d.id
GROUP BY d.name;
这个查询将返回以下结果:
DEPARTMENT_NAME EMPLOYEE_COUNT
--------------- --------------
HR 2
IT 1
在这个示例中,我们使用了INNER JOIN将employees表和departments表连接起来,然后使用GROUP BY子句按部门名称分组,并使用COUNT函数统计每个部门的员工数量。你可以根据需要修改查询以满足你的统计需求。