Oracle SQL-用最早的日期更新行中的2列

时间:2018-09-30 18:09:56

标签: sql oracle

我正在尝试连续更新2列。应该更新的行是最早的duedate

的行。

表的补全描述为:

 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 CHOREID                                   NOT NULL NUMBER(38)
 GROUPID                                   NOT NULL NUMBER(38)
 DUEDATE                                   NOT NULL DATE
 COMPLETEDDATE                                      DATE
 COMPLETEDBY                                        NUMBER(38)

此查询返回我要更新的行

select *
from 
(
        select choreid, duedate, row_number() 
        over (partition by choreid order by duedate) as rn 
        from chorecompletion where choreid = 12 and groupid = 6
)
where rn = 1;

我可以在其中使用帮助的地方是如何在我的update语句中使用此查询,特别是在where子句中

我当前的尝试:

update chorecompletion 
set completeddate = sysdate, completedby=1
where --How to get the result of the previous query here?

对我的逻辑的任何帮助将不胜感激。

所需结果示例:

更新前:

CHOREID      GROUPID    DUEDATE     COMPLETEDDATE      COMPLETEDBY
-------------------------------------------------------------------
  12          6        2018-11-1
  12          6        2018-10-1

更新后

CHOREID      GROUPID    DUEDATE     COMPLETEDDATE      COMPLETEDBY
-------------------------------------------------------------------
  12          6        2018-11-1 
  12          6        2018-10-1      2018-09-30            1

3 个答案:

答案 0 :(得分:2)

像这样吗?

SQL> create table test
  2  (choreid number,
  3   groupid number,
  4   duedate date,
  5   completeddate date,
  6   completedby number
  7  );

Table created.

SQL> insert into test
  2    select 12, 6, date '2018-01-11', null, null from dual union all
  3    select 12, 6, date '2018-01-10', null, null from dual;

2 rows created.

SQL> update test t set
  2    t.completeddate = sysdate,
  3    t.completedby = 1
  4  where t.duedate = (select min(t1.duedate)
  5                     from test t1
  6                     where t1.choreid = t.choreid
  7                       and t1.groupid = t.groupid)
  8    and t.choreid = 12
  9    and t.groupid = 6;

1 row updated.

SQL> select * From test;

   CHOREID    GROUPID DUEDATE    COMPLETEDD COMPLETEDBY
---------- ---------- ---------- ---------- -----------
        12          6 2018-01-11
        12          6 2018-01-10 2018-09-30           1

SQL>

答案 1 :(得分:1)

您可以使用MERGE语句并可以加入ROWID伪列,以便可以直接与匹配的行相关: SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE chorecompletion ( choreid, groupid, duedate, completeddate, completedby ) AS
  SELECT 12, 6, DATE '2018-09-29', CAST( null AS DATE ), CAST( null AS NUMBER ) FROM DUAL UNION ALL
  SELECT 12, 6, DATE '2018-09-30', null, null FROM DUAL;

查询1

MERGE INTO chorecompletion dst
USING (
  SELECT ROWID AS rid
  FROM   (
    SELECT *
    FROM   chorecompletion
    WHERE  choreid = 12
    AND    groupid = 6
    ORDER BY duedate ASC
  )
  WHERE ROWNUM = 1
) src
ON ( src.RID = dst.ROWID )
WHEN MATCHED THEN
  UPDATE
  SET completeddate = sysdate,
      completedby   = 1

Results

1 Row Updated.

查询2

SELECT * FROM chorecompletion

Results

| CHOREID | GROUPID |              DUEDATE |        COMPLETEDDATE | COMPLETEDBY |
|---------|---------|----------------------|----------------------|-------------|
|      12 |       6 | 2018-09-29T00:00:00Z | 2018-09-30T18:42:45Z |           1 |
|      12 |       6 | 2018-09-30T00:00:00Z |               (null) |      (null) |

查询3 :您还可以在UPDATE伪列中使用ROWID语句:

UPDATE chorecompletion dst
SET    completeddate = sysdate,
       completedby   = 2
WHERE  ROWID = (
  SELECT ROWID
  FROM   (
    SELECT ROW_NUMBER() OVER ( PARTITION BY choreid ORDER BY duedate ) rn
    FROM   chorecompletion
    WHERE  choreid = 12
    AND    groupid = 6
    ORDER BY duedate ASC
  )
  WHERE rn = 1
)

Results

1 Row Updated.

查询4

SELECT * FROM chorecompletion

Results

| CHOREID | GROUPID |              DUEDATE |        COMPLETEDDATE | COMPLETEDBY |
|---------|---------|----------------------|----------------------|-------------|
|      12 |       6 | 2018-09-29T00:00:00Z | 2018-09-30T18:42:45Z |           2 |
|      12 |       6 | 2018-09-30T00:00:00Z |               (null) |      (null) |

答案 2 :(得分:0)

您可以使用相关子查询。如果我理解正确:

update chorecompletion
    set completeddate = (select min(duedate)
                         from chorecompletion cc
                         where cc.choreid = chorecompletion.coreid
                        )
    where choreid = 12 and groupid = 6