使用聚合自联接更新查询
我正在使用自连接来获取年初至今的总数。这有效:
SELECT tc.EmployeeID, tc.TimeCardNum, tc.Tax, SUM(inr.Tax) AS YTDTax
FROM TimeCards tc
JOIN (
SELECT EmployeeID, TimeCardNum, Tax
FROM TimeCards
) AS inr
ON inr.EmployeeID = tc.EmployeeID
AND inr.TimeCardNum <= tc.TimeCardNum
GROUP BY tc.EmployeeID, tc.TimeCardNum, tc.Tax
ORDER BY tc.EmployeeID, tc.TimeCardNum
这给了我一个为每个员工重置的运行总计列。
现在我想将其转换为 UPDATE 查询以将该数据放入新列中。这是怎么做到的?
I am using a self-join to get year-to-date totals. This works:
SELECT tc.EmployeeID, tc.TimeCardNum, tc.Tax, SUM(inr.Tax) AS YTDTax
FROM TimeCards tc
JOIN (
SELECT EmployeeID, TimeCardNum, Tax
FROM TimeCards
) AS inr
ON inr.EmployeeID = tc.EmployeeID
AND inr.TimeCardNum <= tc.TimeCardNum
GROUP BY tc.EmployeeID, tc.TimeCardNum, tc.Tax
ORDER BY tc.EmployeeID, tc.TimeCardNum
which gives me a running total column that resets for each employee.
Now I want to turn it into an UPDATE
query to put that data into a new column. How is this done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过:
在事务中运行以确保它产生所需的输出。
Have you tried:
Run in a transaction to ensure it produces the desired output.