java 查询 父子部门的人数 ,父部门人数 为各子部门人数总和
时间: 2023-11-28 22:50:50 浏览: 105
假设你的部门表结构如下:
```
CREATE TABLE department (
id INT PRIMARY KEY,
name VARCHAR(50),
parent_id INT
);
```
其中,`parent_id` 表示父部门的 ID,如果是顶级部门,则为 `NULL`。
可以使用递归查询来完成这个任务。具体实现如下:
```
WITH RECURSIVE cte AS (
-- 查询所有顶级部门的 ID 和人数
SELECT d.id, COUNT(*) AS count
FROM department d
LEFT JOIN department sub ON d.id = sub.parent_id
WHERE d.parent_id IS NULL
GROUP BY d.id
UNION ALL
-- 递归查询子部门的 ID 和人数
SELECT d.id, COUNT(*) AS count
FROM department d
JOIN cte ON d.parent_id = cte.id
GROUP BY d.id
)
-- 最终结果为各部门的 ID 和人数
SELECT id, SUM(count) AS count
FROM cte
GROUP BY id;
```
上述 SQL 查询语句中,使用了“递归公共表表达式”(CTE)来实现递归查询。首先查询所有顶级部门的 ID 和人数,然后递归查询各子部门的 ID 和人数,最终汇总各部门的 ID 和人数。
相关问题
java 实现 父子部门的人数 ,父部门人数 为各子部门人数总和
可以使用树形数据结构来表示部门结构,每个节点表示一个部门,包括部门名、部门人数和子部门列表。然后从根节点开始遍历整个树,统计每个节点的人数,并将其加到其父节点的人数中。具体实现可以参考以下代码:
```java
class Department {
String name; // 部门名
int count; // 部门人数
List<Department> children; // 子部门列表
public Department(String name, int count) {
this.name = name;
this.count = count;
this.children = new ArrayList<>();
}
public void addChild(Department child) {
children.add(child);
}
public int getTotalCount() {
int totalCount = count;
for (Department child : children) {
totalCount += child.getTotalCount();
}
return totalCount;
}
}
public class DepartmentCount {
public static void main(String[] args) {
Department root = new Department("总公司", 0);
Department dep1 = new Department("研发部", 10);
Department dep2 = new Department("销售部", 20);
Department dep3 = new Department("市场部", 15);
root.addChild(dep1);
root.addChild(dep2);
root.addChild(dep3);
Department dep11 = new Department("前端组", 5);
Department dep12 = new Department("后端组", 5);
dep1.addChild(dep11);
dep1.addChild(dep12);
System.out.println(root.getTotalCount()); // 输出总公司人数
}
}
```
在这个例子中,我们创建了一个根节点表示总公司,它包含三个子节点表示研发部、销售部和市场部。研发部又包含前端组和后端组两个子部门。调用根节点的 `getTotalCount()` 方法可以得到总公司的人数,该方法会递归遍历整个树,统计每个节点的人数。
java 实现 父子部门的人数 ,父部门人数 为各子部门人数总和 ,和输出各自节点人数
假设我们有一个部门树形结构,每个节点包含一个部门对象和一个子节点列表。部门对象中包含一个部门名称和一个人数。
首先我们需要定义一个递归函数来计算每个节点的人数:
```java
public int calculateTotalEmployees(DepartmentNode node) {
int total = node.getDepartment().getEmployeeCount();
for (DepartmentNode child : node.getChildren()) {
total += calculateTotalEmployees(child);
}
node.getDepartment().setEmployeeCount(total); // 更新当前节点的员工数
return total;
}
```
这个函数首先会获取当前节点的员工数,然后递归调用子节点的这个函数,并将子节点的返回值累加到总人数中。最后更新当前节点的员工数并返回总人数。
接下来我们可以遍历整个部门树,对每个节点调用上面的函数来计算员工数:
```java
public void printDepartmentEmployeeCount(DepartmentNode root) {
calculateTotalEmployees(root); // 先计算每个节点的员工数
printEmployeeCount(root); // 输出每个节点的员工数
}
private void printEmployeeCount(DepartmentNode node) {
System.out.println(node.getDepartment().getName() + ": " + node.getDepartment().getEmployeeCount());
for (DepartmentNode child : node.getChildren()) {
printEmployeeCount(child);
}
}
```
这个函数会先调用 `calculateTotalEmployees` 函数来计算每个节点的员工数,然后遍历整个部门树,输出每个节点的员工数。注意输出时要输出节点名称和员工数。
完整代码如下:
```java
public class DepartmentNode {
private Department department;
private List<DepartmentNode> children;
public DepartmentNode(Department department) {
this.department = department;
children = new ArrayList<>();
}
public Department getDepartment() {
return department;
}
public void addChild(DepartmentNode child) {
children.add(child);
}
public List<DepartmentNode> getChildren() {
return children;
}
}
public class Department {
private String name;
private int employeeCount;
public Department(String name, int employeeCount) {
this.name = name;
this.employeeCount = employeeCount;
}
public String getName() {
return name;
}
public int getEmployeeCount() {
return employeeCount;
}
public void setEmployeeCount(int employeeCount) {
this.employeeCount = employeeCount;
}
}
public class Main {
public static void main(String[] args) {
DepartmentNode root = new DepartmentNode(new Department("总部", 0));
DepartmentNode child1 = new DepartmentNode(new Department("子部门1", 10));
DepartmentNode child2 = new DepartmentNode(new Department("子部门2", 20));
DepartmentNode child3 = new DepartmentNode(new Department("子部门3", 30));
root.addChild(child1);
root.addChild(child2);
root.addChild(child3);
DepartmentNode grandChild1 = new DepartmentNode(new Department("孙子部门1", 5));
DepartmentNode grandChild2 = new DepartmentNode(new Department("孙子部门2", 15));
DepartmentNode grandChild3 = new DepartmentNode(new Department("孙子部门3", 25));
child1.addChild(grandChild1);
child2.addChild(grandChild2);
child3.addChild(grandChild3);
Main main = new Main();
main.printDepartmentEmployeeCount(root);
}
public int calculateTotalEmployees(DepartmentNode node) {
int total = node.getDepartment().getEmployeeCount();
for (DepartmentNode child : node.getChildren()) {
total += calculateTotalEmployees(child);
}
node.getDepartment().setEmployeeCount(total); // 更新当前节点的员工数
return total;
}
public void printDepartmentEmployeeCount(DepartmentNode root) {
calculateTotalEmployees(root); // 先计算每个节点的员工数
printEmployeeCount(root); // 输出每个节点的员工数
}
private void printEmployeeCount(DepartmentNode node) {
System.out.println(node.getDepartment().getName() + ": " + node.getDepartment().getEmployeeCount());
for (DepartmentNode child : node.getChildren()) {
printEmployeeCount(child);
}
}
}
```
阅读全文