通过 CTE 或子查询与其下属更新记录
假设我有一个包含以下列的表:
Employees 表
employeeID int
employeeName varchar(50)
managerID int
totalOrganization int
managerID 引用了 employeeID。目前所有记录的totalOrganization 均为0。
我想将每行的 TotalOrganization 更新为其下的员工总数。
因此,使用以下记录:
employeeID employeeName managerID totalOrganization
1 John Cruz NULL 0
2 Mark Russell 1 0
3 Alice Johnson 1 0
4 Juan Valdez 3 0
查询应将totalOrganizations 更新为:
employeeID employeeName managerID totalOrganization
1 John Cruz NULL 3
2 Mark Russell 1 0
3 Alice Johnson 1 1
4 Juan Valdez 3 0
我知道我可以获得一些组织。使用以下 CTE 的图表:
WITH OrgChart (employeeID, employeeName,managerID,level)
AS (
SELECT employeeID,employeeName,0 as managerID,0 AS Level
FROM Employees
WHERE managerID IS NULL
UNION ALL
SELECT Employees.employeeID,Employees.employeeName,Employees.managerID,Level + 1
FROM Employees INNER JOIN
OrgChart ON Employees.managerID = OrgChart.employeeID
)
SELECT employeeID,employeeName,managerID, level
FROM OrgChart;
是否有任何方法可以使用存储过程来更新员工表,而不是在 SQL 之外构建一些例程来解析数据?
Let's say I have a table with the following columns:
Employees Table
employeeID int
employeeName varchar(50)
managerID int
totalOrganization int
managerID is referential to employeeID. totalOrganization is currently 0 for all records.
I'd like to update totalOrganization on each row to the total number of employees under them.
So with the following records:
employeeID employeeName managerID totalOrganization
1 John Cruz NULL 0
2 Mark Russell 1 0
3 Alice Johnson 1 0
4 Juan Valdez 3 0
The query should update the totalOrganizations to:
employeeID employeeName managerID totalOrganization
1 John Cruz NULL 3
2 Mark Russell 1 0
3 Alice Johnson 1 1
4 Juan Valdez 3 0
I know I can get somewhat of an org. chart using the following CTE:
WITH OrgChart (employeeID, employeeName,managerID,level)
AS (
SELECT employeeID,employeeName,0 as managerID,0 AS Level
FROM Employees
WHERE managerID IS NULL
UNION ALL
SELECT Employees.employeeID,Employees.employeeName,Employees.managerID,Level + 1
FROM Employees INNER JOIN
OrgChart ON Employees.managerID = OrgChart.employeeID
)
SELECT employeeID,employeeName,managerID, level
FROM OrgChart;
Is there any way to update the Employees table using a stored procedure rather than building some routine outside of SQL to parse through the data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过几个小时的实验,我得出了以下结论。它给出了所需的结果。有人看到改进的方法吗?
After a few hours of experimentation I came up with the following. It gives the desired results. Anyone see a way to improve it?
您可以添加另一个 CTE 来确定员工数量,然后在 Update 语句中使用它:
ADDITION
规范中的更改是您需要所有下级员工的数量。做到这一点的诀窍是为员工创建一条通往每位经理的路径。因此,首先这是我的测试数据:
因此,首先我们编写一个查询,为我们提供通往其经理的路径:
生成:
现在我们只需计算给定员工存在于某人路径中的实例:
我从总数中减去 1排除当前员工。现在我们已经找到了一个可以提供正确结果的查询,我们可以轻松地使用它来进行更新:
You can add another CTE to determine the count of employees and then use that in an Update statement:
ADDITION
The change in spec is that you want a count of all subordinate employees. The trick to that is to create a path of the employee to each of their managers. So, first here is my test data:
So, first we write a query that gives us a path to their manager:
That produces:
Now we simply need to count instances where the given employee exists in someone's path:
I subtract one from the total to exclude the current employee. Now that we've found a query to provide the proper results, we can easily use that to do an update:
这(当然)可以在存储过程中完成。然而,它看起来很像不能用单个(CTE)语句来完成,因为您无法对给定员工的下属+他们的所有下属进行求和(即计算给定项目下的所有后代)层次结构),按照此错误消息:
因此,必须编写在 SQL 之外编写的例程(从层次结构的最低“级别”开始,计算所有员工下属,在迭代层次结构时重复)在 SQL 中。
This can (of course) be done within a stored procedure. However, it looks very much like it cannot be done with a single (CTE) statement, as you cannot sum a given employee's subordinates + all of their subordinates (i.e. tally all descendants underneath a given item in the hierarchy), as per this error message:
So that routine you'd write outside of SQL (start at the lowest "level" of the hierarchy, count all those employees subordinates, repeat as you iterate up the hierarchy) would have to be written within SQL.