繁体   English   中英

在Oracle SQL中,如何更新优先级列表指定的列?

[英]In Oracle SQL, how do I UPDATE columns specified by a priority list?

因此,我已经回答了这个问题,但是现在我需要Oracle SQL解决方案。 (请参阅: 具有多个条件的更新。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子句。

这是我本质上需要在UPDATE中使用的工作查询,并将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

假设提供的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);
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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM