使用 Oracle 中的子查询分组的值有条件地更新表

发布于 2024-08-31 12:03:15 字数 903 浏览 5 评论 0原文

问题是,将参与过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女皇必胜 2024-09-07 12:03:15

如果 Oracle 中的联接包含 GROUP BY 子句,您将无法更新该联接。您可以使用内联子查询,如下所示:

UPDATE employee e
   SET sal = sal * (SELECT CASE
                              WHEN COUNT(*) >= 5 THEN
                               1.5
                              WHEN COUNT(*) >= 3 THEN
                               1.3
                              WHEN COUNT(*) >= 1 THEN
                               1.2
                              ELSE
                               1
                           END
                      FROM employee_project_history eph
                     WHERE eph.empno = e.empno);

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:

UPDATE employee e
   SET sal = sal * (SELECT CASE
                              WHEN COUNT(*) >= 5 THEN
                               1.5
                              WHEN COUNT(*) >= 3 THEN
                               1.3
                              WHEN COUNT(*) >= 1 THEN
                               1.2
                              ELSE
                               1
                           END
                      FROM employee_project_history eph
                     WHERE eph.empno = e.empno);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文