使用 Oracle 中的子查询分组的值有条件地更新表
问题是,将参与过 5 个或 5 个以上项目的所有员工的工资更新为 50%,更新为 30%(>= 3 个项目),更新为 20%(>= 1 个项目),项目数量为通过对 EMPLOYEE_PROJECT_HISTORY 执行分组查询得到;
我已经尝试过这些查询
update emp set emp.sal=
case
when jemp.pcount >=5 then emp.sal+ (emp.sal*50)/100
when jemp.pcount >=3 then emp.sal+ (emp.sal*30)/100
when jemp.pcount >=1 then emp.sal+ (emp.sal*20)/100
else emp.sal+ (emp.sal*20)/100
end
from employee emp join (select empno as jempno,count(projectno) as pcount from EMPLOYEE_PROJECT_HISTORY by empno) jemp on emp.empno=jemp.jempno ;
update employee a set a.sal= case (select count(b.projectno) as pcount from EMPLOYEE_PROJECT_HISTORY b group by b.empno )
when b.pcount >5 then
a.sal = a.sal+ (a.sal*50)/100
when pcount >3 then
a.sal = a.sal+ (a.sal*30)/100
when pcount >1 then
a.sal = a.sal+ (a.sal*20)/100
end;
the problem is Update the salary of all the employees by 50% who had worked on 5 or more than 5 projects, by 30% (>= 3 projects), by 20 % (>= 1 projects) the number of project is got by performing a group by query on the EMPLOYEE_PROJECT_HISTORY;
i have tried these queries
update emp set emp.sal=
case
when jemp.pcount >=5 then emp.sal+ (emp.sal*50)/100
when jemp.pcount >=3 then emp.sal+ (emp.sal*30)/100
when jemp.pcount >=1 then emp.sal+ (emp.sal*20)/100
else emp.sal+ (emp.sal*20)/100
end
from employee emp join (select empno as jempno,count(projectno) as pcount from EMPLOYEE_PROJECT_HISTORY by empno) jemp on emp.empno=jemp.jempno ;
update employee a set a.sal= case (select count(b.projectno) as pcount from EMPLOYEE_PROJECT_HISTORY b group by b.empno )
when b.pcount >5 then
a.sal = a.sal+ (a.sal*50)/100
when pcount >3 then
a.sal = a.sal+ (a.sal*30)/100
when pcount >1 then
a.sal = a.sal+ (a.sal*20)/100
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 Oracle 中的联接包含 GROUP BY 子句,您将无法更新该联接。您可以使用内联子查询,如下所示:
You won't be able to update the join if it contains a GROUP BY clause in Oracle. You can use an inline subquery, like this: