因此,我已经回答了这个问题,但是现在我需要Oracle SQL解决方案。(见:An update with multiple conditions. SQL 2008)
但要再经历一次..。
下面是当前的表"table1“。
ProjectID UserID RoleID
101 1 10
101 2 10
102 2 10
102 3 10
103 1 10
目前只有一种类型的角色,角色'10',但我想添加一个新的角色,角色'11',这将作为一个领导。因此,任何具有'10‘角色的用户的项目,都应该有一个领导。被选择为领导者的用户将基于一个优先级列表,在本例中,我们将说顺序是1、2、3。
预期结果..。
ProjectID UserID RoleID
101 1 11
101 2 10
102 2 11
102 3 10
103 1 11
从前面的解决方案中,我无法使WITH子句正常工作,正如我所了解到的,Oracle不会在WITH子句中使用from。
下面是我本质上需要在更新中使用的工作查询,并将roleid更新为11,其中PriorityForLead = 1。
select t.*, row_number() over (partition by projectid
order by (case when userid = 1 then 1
when userid = 2 then 2
when userid = 3 then 3
else 4
end )
) as PriorityForLead
from table1 t
发布于 2014-09-03 19:37:01
update table1 t1
set roleid = 11
where roleid = 10 and
(case when userid = 1 then 1 when userid = 2 then 2 when userid = 3 then 3 else 4 end) =
(select min(case when userid = 1 then 1 when userid = 2 then 2 when userid = 3 then 3 else 4 end)
from table1
where projectid = t1.projectid);
编辑:
SQL> create table table1 (projectid number, userid number, roleid number);
Table created.
SQL> insert into table1 values (101, 1, 10);
1 row created.
SQL> insert into table1 values (101, 2, 10);
1 row created.
SQL> insert into table1 values (102, 2, 10);
1 row created.
SQL> insert into table1 values (102, 3, 10);
1 row created.
SQL> insert into table1 values (103, 1, 10);
1 row created.
SQL> select * from table1;
PROJECTID USERID ROLEID
---------- ---------- ----------
101 1 10
101 2 10
102 2 10
102 3 10
103 1 10
SQL> update table1 t1
2 set roleid = 11
3 where roleid = 10 and
4 (case when userid = 1 then 1 when userid = 2 then 2 when userid = 3 then 3 else 4 end) =
5 (select min(case when userid = 1 then 1 when userid = 2 then 2 when userid = 3
then 3 else 4 end)
5 from table1
6 where projectid = t1.projectid);
3 rows updated.
SQL> select * from table1;
PROJECTID USERID ROLEID
---------- ---------- ----------
101 1 11
101 2 10
102 2 11
102 3 10
103 1 11
发布于 2014-09-03 19:35:18
假设提供的3列是此表的复合主键,下面的查询应该是对Oracle的正确转换。
update t
set RoleId = 11
WHERE EXISTS (SELECT 1 FROM
(select t.*,
row_number() over (partition by projectid
order by (case when userid = 1 then 1
when userid = 2 then 2
when userid = 3 then 3
else 4
end
)
) as PriorityForLead
from table t) toupdate
WHERE toupdate.PriorityForLead = 1
AND t.ProjectID = toupdate.ProjectID
AND t.UserID = toupdate.UserID
AND t.RoleID = toupdate.RoleId);
https://stackoverflow.com/questions/25652248
复制相似问题