首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Oracle SQL中,如何更新优先级列表指定的列?

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

Stack Overflow用户
提问于 2014-09-03 19:27:53
回答 2查看 137关注 0票数 0

因此,我已经回答了这个问题,但是现在我需要Oracle SQL解决方案。(见:An update with multiple conditions. SQL 2008)

但要再经历一次..。

下面是当前的表"table1“。

代码语言:javascript
运行
复制
ProjectID    UserID    RoleID
101          1         10
101          2         10
102          2         10
102          3         10
103          1         10

目前只有一种类型的角色,角色'10',但我想添加一个新的角色,角色'11',这将作为一个领导。因此,任何具有'10‘角色的用户的项目,都应该有一个领导。被选择为领导者的用户将基于一个优先级列表,在本例中,我们将说顺序是1、2、3。

预期结果..。

代码语言:javascript
运行
复制
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。

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-03 19:37:01

代码语言:javascript
运行
复制
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);

编辑:

代码语言:javascript
运行
复制
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
票数 0
EN

Stack Overflow用户

发布于 2014-09-03 19:35:18

假设提供的3列是此表的复合主键,下面的查询应该是对Oracle的正确转换。

代码语言:javascript
运行
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25652248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档