oracle 如何将单个记录拆分为多个记录

laawzig2  于 2023-04-29  发布在  Oracle
关注(0)|答案(2)|浏览(125)

我有下面的表,我需要复制的数据从第一表的日志表

CREATE TABLE datatable
(
   "DT_ID"                NUMBER (10, 0),
   "ASSIGNED"             NUMBER (1, 0) DEFAULT 0,
   "ASSIGNED_DATE_TIME"   DATE,
   "ASSIGNED_BY"          VARCHAR2 (7 BYTE),
   "ACCEPTED"             NUMBER (1, 0) DEFAULT 0,
   "ACCEPTED_DATE_TIME"   DATE,
   "ACCEPTED_BY"          VARCHAR2 (7 BYTE),
   "REJECTED"             NUMBER (1, 0) DEFAULT 0,
   "REJECTED_DATE_TIME"   DATE,
   "REJECTED_BY"          VARCHAR2 (7 BYTE)
);

CREATE TABLE logtable
(
   dt_id            NUMBER (10, 0),      -- ID FROM dATATABLE
   "ACTION_OWNER"   NVARCHAR2 (50),      --"ASSIGNED_BY"
   "ACTION"         VARCHAR2 (50 BYTE),  -- ASSIGNED/REJECTED/ACCEPTED
   "ACTION_DATE"    TIMESTAMP (3),       -- ASSIGNED_DATE_TIME/ACCEPTED_DATE_TIME/REJECTED_DATE_TIME
   "REMARK"         VARCHAR2 (200 BYTE)
);

这里的第二个表是一个日志表,我们需要将第一个表中的一行转换为第二个表中的多行

44u64gxh

44u64gxh1#

你在找这样的东西吗?循环data_table中的所有记录,并根据assigned/accepted/rejected的值插入logtable。代码未经测试。

BEGIN
  FOR r IN (SELECT * FROM datatable) LOOP
    IF r.assigned = 1 THEN
      INSERT INTO logtable(dt_id,action_owner,action,action_date)
        VALUES (r.dt_id, r.assigned_by,'ASSIGNED',r.assigned_date_time);
    END IF;
    IF r.accepted = 1 THEN
      INSERT INTO logtable(dt_id,action_owner,action,action_date)
        VALUES (r.dt_id, r.accepted_by,'ACCEPTED',r.accepted_date_time);
    END IF;
    IF r.rejected = 1 THEN
      INSERT INTO logtable(dt_id,action_owner,action,action_date)
        VALUES (r.dt_id, r.rejected_by,'REJECTED',r.rejected_date_time);
    END IF;
  END LOOP;
END;
wvyml7n5

wvyml7n52#

假设您的数据如下所示:

WITH       -- Sample data
    tbl (   DT_ID, 
            ASSIGNED, ASSIGNED_DATE_TIME, ASSIGNED_BY, 
            ACCEPTED, ACCEPTED_DATE_TIME, ACCEPTED_BY, 
            REJECTED, REJECTED_DATE_TIME, REJECTED_BY
        ) AS
            (   Select  1, 
                        1, To_Date('01.01.2023', 'dd.mm.yyyy'), 'Marcus',
                        1, To_Date('02.01.2023', 'dd.mm.yyyy'), 'John',
                        0, Null, Null
                From Dual Union All
                --
                Select  2, 
                        1, To_Date('01.02.2023', 'dd.mm.yyyy'), 'Maria',
                        0, Null, Null,
                        1, To_Date('02.02.2023', 'dd.mm.yyyy'), 'John'
                From Dual Union All
                --
                Select  3, 
                        1, To_Date('01.03.2023', 'dd.mm.yyyy'), 'Peter',
                        1, To_Date('02.03.2023', 'dd.mm.yyyy'), 'Mike',
                        1, To_Date('03.03.2023', 'dd.mm.yyyy'), 'Supervisor'
                From Dual 
            )
Select * From tbl    -- your table data
     DT_ID   ASSIGNED ASSIGNED_ ASSIGN   ACCEPTED ACCEPTED_ ACCEPTED_BY   REJECTED REJECTED_ REJECTED_B
---------- ---------- --------- ------ ---------- --------- ----------- ---------- --------- ----------
         1          1 01-JAN-23 Marcus          1 02-JAN-23 John                 0                     
         2          1 01-FEB-23 Maria           0                                1 02-FEB-23 John      
         3          1 01-MAR-23 Peter           1 02-MAR-23 Mike                 1 03-MAR-23 Supervisor

您可以使用UNION ALL将数据选择到多行中,如下所示:

--  Main SQL
SELECT DT_ID, ACTION_OWNER, ACTION, ACTION_DATE, REMARK 
FROM  ( Select  DT_ID, ASSIGNED_BY "ACTION_OWNER", 'ASSIGNED' "ACTION", ASSIGNED_DATE_TIME "ACTION_DATE", Null "REMARK" From tbl Union All
        Select  DT_ID, ACCEPTED_BY "ACTION_OWNER", 'ACCEPTED' "ACTION", ACCEPTED_DATE_TIME "ACTION_DATE", Null "REMARK" From tbl Union All
        Select  DT_ID, REJECTED_BY "ACTION_OWNER", 'REJECTED' "ACTION", REJECTED_DATE_TIME "ACTION_DATE", Null "REMARK" From tbl 
      )
WHERE ACTION_OWNER Is NOT Null -- exclude incomplete rows if you wish so
ORDER BY DT_ID, ACTION_DATE    -- Order by is irrelevant for insertion
--
--  R e s u l t :
     DT_ID ACTION_OWNER ACTION   ACTION_DA REMARK
---------- ------------ -------- --------- -----------------------
         1 Marcus       ASSIGNED 01-JAN-23       
         1 John         ACCEPTED 02-JAN-23       
         2 Maria        ASSIGNED 01-FEB-23       
         2 John         REJECTED 02-FEB-23       
         3 Peter        ASSIGNED 01-MAR-23       
         3 Mike         ACCEPTED 02-MAR-23       
         3 Supervisor   REJECTED 03-MAR-23

如果结果对您有利-您可以将这些行插入日志表中:

INSERT INTO log_tbl (DT_ID, ACTION_OWNER, ACTION, ACTION_DATE, REMARK) 
... Main SQL from above ...

相关问题