如何在满足给定条件的许多行中更新除一行之外的所有行?

时间:2015-07-23 10:05:24

标签: sql oracle oracle11g sql-update

我有一个scenerio,我必须更新所有行,但有很多行中的一行。我有一个像

的表
__________________________________________________________
|COlA         | COLB     | COLC    |  COLD   |   COLE  |
-----------------------------------------------------------
|Equipment SI | ADD INFO | MERGE   | Notes   |    Y    |
|Equipment SI | Active   | MERGE   | Notes   |    Y    |
|Equipment SI | ORIGINAL | MERGE   | Notes   |    Y    |
|Fastening    | ADD INFO | MERGE   | Notes   |    Y    |
|Fastening    | Active   | MERGE   | Notes   |    Y    |
|Electonics   | ADD INFO | MERGE   | Notes   |    Y    |
|Electonics   | Active O | MERGE   | Notes   |    Y    |
|Electonics   | ORIGINAL | MERGE   | Notes   |    Y    |
|Electonics   | Nominated| MERGE   | Notes   |    Y    |
|Fiber        | ADD INFO | MERGE   | Notes   |    Y    |
|Fiber        | ADD INFO | MULTI   | Notes   |    Y    |
|Fiber        | ADD INFO | KILO    | Notes   |    Y    |

现在我需要得到像

这样的输出
__________________________________________________________
|COlA         | COLB     | COLC    |  COLD   |   COLE  |
-----------------------------------------------------------
|Equipment SI | ADD INFO | MERGE   | Notes   |    Y    |
|Equipment SI | Active   | MERGE   | Notes   |    N    |
|Equipment SI | ORIGINAL | MERGE   | Notes   |    N    |
|Fastening    | ADD INFO | MERGE   | Notes   |    Y    |
|Fastening    | Active   | MERGE   | Notes   |    N    |
|Electonics   | ADD INFO | MERGE   | Notes   |    Y    |
|Electonics   | Active O | MERGE   | Notes   |    N    |
|Electonics   | ORIGINAL | MERGE   | Notes   |    N    |
|Electonics   | Nominated| MERGE   | Notes   |    N    |
|Fiber        | ADD INFO | MERGE   | Notes   |    Y    |
|Fiber        | ADD INFO | MULTI   | Notes   |    Y    |
|Fiber        | ADD INFO | KILO    | Notes   |    Y    |

我正在尝试从'Y'更新COLE到'N'(除了一行)。COLA,COLD,COLE应该相同以更新该特定集。如果任何行(COLA,COLC,COLD)组合只有一个'Y',那么我不应该更新任何东西(我的样本数据中的光纤)。我必须更新整个表。有人可以帮我这个。我应该创建一个函数并循环它吗?那也是如何只更新一行?

2 个答案:

答案 0 :(得分:4)

由于您在更新表时不关心行的排序,因此您只需使用 MIN GROUP BY

更新您需要按colAcolC进行分组。

例如,

<强>设置

SQL> CREATE TABLE t
  2    (
  3      COlA VARCHAR2(12),
  4      COLB VARCHAR2(9),
  5      COLC VARCHAR2(5),
  6      COLD VARCHAR2(5),
  7      COLE VARCHAR2(1)
  8    );

Table created.

SQL> INSERT ALL
  2      INTO t (COlA, COLB, COLC, COLD, COLE)
  3           VALUES ('Equipment SI', 'ADD INFO', 'MERGE', 'Notes', 'Y')
  4      INTO t (COlA, COLB, COLC, COLD, COLE)
  5           VALUES ('Equipment SI', 'Active', 'MERGE', 'Notes', 'Y')
  6      INTO t (COlA, COLB, COLC, COLD, COLE)
  7           VALUES ('Equipment SI', 'ORIGINAL', 'MERGE', 'Notes', 'Y')
  8      INTO t (COlA, COLB, COLC, COLD, COLE)
  9           VALUES ('Fastening', 'ADD INFO', 'MERGE', 'Notes', 'Y')
 10      INTO t (COlA, COLB, COLC, COLD, COLE)
 11           VALUES ('Fastening', 'Active', 'MERGE', 'Notes', 'Y')
 12      INTO t (COlA, COLB, COLC, COLD, COLE)
 13           VALUES ('Electonics', 'ADD INFO', 'MERGE', 'Notes', 'Y')
 14      INTO t (COlA, COLB, COLC, COLD, COLE)
 15           VALUES ('Electonics', 'Active O', 'MERGE', 'Notes', 'Y')
 16      INTO t (COlA, COLB, COLC, COLD, COLE)
 17           VALUES ('Electonics', 'ORIGINAL', 'MERGE', 'Notes', 'Y')
 18      INTO t (COlA, COLB, COLC, COLD, COLE)
 19           VALUES ('Electonics', 'Nominated', 'MERGE', 'Notes', 'Y')
 20      INTO t (COlA, COLB, COLC, COLD, COLE)
 21           VALUES ('Fiber', 'ADD INFO', 'MULTI', 'Notes', 'Y')
 22      INTO t (COlA, COLB, COLC, COLD, COLE)
 23           VALUES ('Fiber', 'ADD INFO', 'KILO', 'Notes', 'Y')
 24  SELECT * FROM dual;

11 rows created.

SQL> COMMIT;

Commit complete.

表格数据

SQL> SELECT * FROM t;

COLA         COLB      COLC  COLD  C
------------ --------- ----- ----- -
Equipment SI ADD INFO  MERGE Notes Y
Equipment SI Active    MERGE Notes Y
Equipment SI ORIGINAL  MERGE Notes Y
Fastening    ADD INFO  MERGE Notes Y
Fastening    Active    MERGE Notes Y
Electonics   ADD INFO  MERGE Notes Y
Electonics   Active O  MERGE Notes Y
Electonics   ORIGINAL  MERGE Notes Y
Electonics   Nominated MERGE Notes Y
Fiber        ADD INFO  MULTI Notes Y
Fiber        ADD INFO  KILO  Notes Y

11 rows selected.

更新声明

SQL> UPDATE t
  2  SET colE         = 'N'
  3  WHERE ROWID NOT IN
  4    ( SELECT MIN(rowid) FROM t GROUP BY colA, colC
  5    );

6 rows updated.

我们检查

SQL> SELECT * FROM t;

COLA         COLB      COLC  COLD  C
------------ --------- ----- ----- -
Equipment SI ADD INFO  MERGE Notes Y
Equipment SI Active    MERGE Notes N
Equipment SI ORIGINAL  MERGE Notes N
Fastening    ADD INFO  MERGE Notes Y
Fastening    Active    MERGE Notes N
Electonics   ADD INFO  MERGE Notes Y
Electonics   Active O  MERGE Notes N
Electonics   ORIGINAL  MERGE Notes N
Electonics   Nominated MERGE Notes N
Fiber        ADD INFO  MULTI Notes Y
Fiber        ADD INFO  KILO  Notes Y

11 rows selected.

SQL>

答案 1 :(得分:1)

update table_name
set colE = 'N'
where rowid not in
(
select min(rowid)
from table_name
group by colA
)