java 查询 父子部门的人数 ,父部门人数 为各子部门人数总和

时间: 2023-11-28 12:50:50 浏览: 32
假设你的部门表结构如下: ``` 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); } } } ```

相关推荐

最新推荐

recommend-type

Oracle通过递归查询父子兄弟节点方法示例

主要给大家介绍了关于Oracle如何通过递归查询父子兄弟节点的相关资料,递归查询对各位程序员来说应该都不陌生,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
recommend-type

vue中如何让子组件修改父组件数据

主要介绍了vue中子组件修改父组件数据的相关资料,文中介绍了vue中watch的认识,关于子组件修改父组件属性认识,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

vue父组件向子组件动态传值的两种方法

主要介绍了vue父组件向子组件动态传值的两种方法 ,需要的朋友可以参考下
recommend-type

Java for循环性能优化实现解析

主要介绍了Java for循环性能优化实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

解决Vue2.x父组件与子组件之间的双向绑定问题

主要介绍了解决Vue2.x父组件与子组件之间的双向绑定问题,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。