Oracle中的临时表问题

dfty9e19  于 2023-11-17  发布在  Oracle
关注(0)|答案(3)|浏览(149)

我们正在使用Db visualizer Pro 12 edition来处理Oracle DB。

TableA
 
 Id   batchId  requestId Filename
 100  Btch100   Rqt1     ABc.zip
 101  Btch100   Null     Abc213.txt
 102  Btch100   Null     Abc5673.pdf
 103  Btch100   Null     Afdc213.txt
 104  Btch101   Rqt2     Abc213.txt
 105  Btch101   Null     Bbc213.txt
 106  Btch102   Null     zbc213.txt
 107  Btch102   Rqt3     z6gt213.txt

字符串
我需要更新表中与同一批ID匹配的requestid

Id   batchId  requestId Filename
 100  Btch100   Rqt1     ABc.zip
 101  Btch100   Rqt1     Abc213.txt
 102  Btch100   Rqt1     Abc5673.pdf
 103  Btch100   Rqt1     Afdc213.txt
 104  Btch101   Rqt2     Abc213.txt
 105  Btch101   Rqt2     Bbc213.txt
 106  Btch102   Rqt3     zbc213.txt
 107  Btch102   Rqt3     z6gt213.txt


我尝试创建私有临时表,根据请求ID和批处理ID选择行,并尝试在保持while循环的同时更新。

CREATE PRIVATE TEMPORARY TABLE temptable AS
SELECT requestId,batchId FROM TableA WHERE requestid is not null  group by requestId,batchId .


我在创建临时表和插入表数据时遇到问题

Error : Unknown objectType 'Private' key word used in Create,Drop,or Alter statement.


请建议是否有任何替代方法来更新表,而不使用temptable

x6492ojm

x6492ojm1#

提示:使用SQL使人们容易回答,例如

create table t ( id int, batchid varchar2(10), requestid varchar2(10), filename varchar2(20));
INSERT INTO t (Id, batchId, requestId, Filename) VALUES (100, 'Btch100', 'Rqt1', 'ABc.zip');
INSERT INTO t (Id, batchId, requestId, Filename) VALUES (101, 'Btch100', Null, 'Abc213.txt');
INSERT INTO t (Id, batchId, requestId, Filename) VALUES (102, 'Btch100', Null, 'Abc5673.pdf');
INSERT INTO t (Id, batchId, requestId, Filename) VALUES (103, 'Btch100', Null, 'Afdc213.txt');
INSERT INTO t (Id, batchId, requestId, Filename) VALUES (104, 'Btch101', 'Rqt2', 'Abc213.txt');
INSERT INTO t (Id, batchId, requestId, Filename) VALUES (105, 'Btch101', Null, 'Bbc213.txt');
INSERT INTO t (Id, batchId, requestId, Filename) VALUES (106, 'Btch102', Null, 'zbc213.txt');
INSERT INTO t (Id, batchId, requestId, Filename) VALUES (107, 'Btch102', 'Rqt3', 'z6gt213.txt');

字符串
现在,我们可以使用LAG将数据级联

SQL> select Id, batchId, requestId, Filename, nvl(lag(requestid ignore nulls) over ( order by id ),requestid) as x
  2  from t;

        ID BATCHID    REQUESTID  FILENAME             X
---------- ---------- ---------- -------------------- ----------
       100 Btch100    Rqt1       ABc.zip              Rqt1
       101 Btch100               Abc213.txt           Rqt1
       102 Btch100               Abc5673.pdf          Rqt1
       103 Btch100               Afdc213.txt          Rqt1
       104 Btch101    Rqt2       Abc213.txt           Rqt1
       105 Btch101               Bbc213.txt           Rqt2
       106 Btch102               zbc213.txt           Rqt2
       107 Btch102    Rqt3       z6gt213.txt          Rqt2

8 rows selected.


然后把结果合并回去

SQL> merge into t
  2  using (
  3    select Id, batchId, requestId, Filename, nvl(lag(requestid ignore nulls) over ( order by id ),requestid) as x
  4    from t ) m
  5  on ( t.id = m.id)
  6  when matched
  7  then update set t.requestid = m.x
  8  where t.requestid is null;

5 rows merged.

SQL>
SQL> select * from t;

        ID BATCHID    REQUESTID  FILENAME
---------- ---------- ---------- --------------------
       100 Btch100    Rqt1       ABc.zip
       101 Btch100    Rqt1       Abc213.txt
       102 Btch100    Rqt1       Abc5673.pdf
       103 Btch100    Rqt1       Afdc213.txt
       104 Btch101    Rqt2       Abc213.txt
       105 Btch101    Rqt2       Bbc213.txt
       106 Btch102    Rqt2       zbc213.txt
       107 Btch102    Rqt3       z6gt213.txt

8 rows selected.

rjzwgtxy

rjzwgtxy2#

您正在查找MAXMIN。('Rqt1',null,null,null的最大值或最小值为'Rqt1'。)
如果你想更新表中的行,那么只需要使用一个简单的UPDATE

update tablea
set requestid = (select max(a.requestid) from tablea a where a.id = tablea.id)
where requestid is null;

字符串
如果只想选择数据,请使用MAX OVER

select
  id,
  batch_id,
  max(requestid) over (partition by batch_id) as requestid,
  filename
from tablea
order by id;

ltskdhd1

ltskdhd13#

其中一个选项是使用MODEL clause

WITH    --  S a m p l e   D a t a :
    tbl (ID, BATCHID, REQUESTID, FILENAME) AS
        (   Select 100, 'Btch100', 'Rqt1', 'ABc.zip' From Dual Union All
            Select 101, 'Btch100', Null, 'Abc213.txt' From Dual Union All
            Select 102, 'Btch100', Null, 'Abc5673.pdf' From Dual Union All
            Select 103, 'Btch100', Null, 'Afdc213.txt' From Dual Union All
            Select 104, 'Btch101', 'Rqt2', 'Abc213.txt' From Dual Union All
            Select 105, 'Btch101', Null, 'Bbc213.txt' From Dual Union All
            Select 106, 'Btch102', Null, 'zbc213.txt' From Dual Union All
            Select 107, 'Btch102', 'Rqt3', 'z6gt213.txt' From Dual 
        )

字符串
.这里是代码

--  M a i n   S Q L :
Select * From tbl
MODEL   Dimension By ( ID, BATCHID )  Measures ( REQUESTID, FILENAME )
RULES   ( REQUESTID[ANY, ANY] = Max(REQUESTID)[ID > 0, BATCHID = CV()] )


... ...这是什么?

/*    R e s u l t :
        ID BATCHID REQUESTID FILENAME   
---------- ------- --------- -----------
       100 Btch100 Rqt1      ABc.zip    
       101 Btch100 Rqt1      Abc213.txt 
       102 Btch100 Rqt1      Abc5673.pdf
       103 Btch100 Rqt1      Afdc213.txt
       104 Btch101 Rqt2      Abc213.txt 
       105 Btch101 Rqt2      Bbc213.txt 
       106 Btch102 Rqt3      zbc213.txt 
       107 Btch102 Rqt3      z6gt213.txt    */


上面的代码可以合并来进行更新:

MERGE INTO tbl t
USING ( Select * From tbl
        MODEL   Dimension By ( ID, BATCHID )  Measures ( REQUESTID, FILENAME )
        RULES   ( REQUESTID[ANY, ANY] = Max(REQUESTID)[ID > 0, BATCHID = CV()] )
      ) x ON(x.ID = t.ID)
WHEN MATCHED THEN
    Update SET t.REQUESTID = x.REQUESTID
WHERE t.REQUESTID Is Null

相关问题