需要帮助以获得所需的输出

时间:2016-06-15 09:09:05

标签: sql oracle plsql oracle11g oracle10g

我需要将某些表格中的数据转换为特定格式。 你能帮我找到想要的输出吗?

以下链接包含一个Excel附件,其中包含以下信息: Google Drive Link

  1. 标签:输入输出 - >包含源和目标表的结构
  2. 标签:创建脚本 - >创建脚本以创建所有表
  3. 标签:插入脚本 - >插入脚本以将数据插入源表
  4. 我尝试过编辑(代码):

    TYPE subline_col_c_rectype IS RECORD
       (
    ID_C    VARCHAR(75), 
    Col_C1  VARCHAR(75), 
    Col_C2 VARCHAR(75)
    
       );
    
       TYPE subline_col_c_tabtype IS TABLE OF subline_col_c_rectype
          INDEX BY BINARY_INTEGER;   
    
    
       TYPE line_rectype IS RECORD
       (
    
       ID_A VARCHAR(75), 
       Col_A1    VARCHAR(75), 
       Col_A2    VARCHAR(75), 
       Col_A3    VARCHAR(75), 
       Col_A4    VARCHAR(75), 
       Col_A5    VARCHAR(75), 
       ID_B  VARCHAR(75), 
       Col_B1    VARCHAR(75), 
       Col_B2    VARCHAR(75), 
       Col_B3    VARCHAR(75), 
       Col_B4    VARCHAR(75), 
        Col_B5 VARCHAR(75), 
        Col_C    subline_col_c_tabtype,
            Col_D    subline_col_c_tabtype
    );
    
       TYPE line_tabtype IS TABLE OF line_rectype
          INDEX BY BINARY_INTEGER;
    

    能够将记录存储在关联数组line_tabtype中,但不确定如何扩展子记录并将其存储在目标表中。

2 个答案:

答案 0 :(得分:1)

SELECT ID_A, COL_A1, COL_A2, COL_A3, COL_A4, COL_A5, ID_B, COL_B1, COL_B2, COL_B3, COL_B4, COL_B5, ID_C, COL_C1, COL_C2, ID_D, COL_D1, COL_D2
  FROM (SELECT a.*, ROW_NUMBER () OVER (PARTITION BY id_a ORDER BY col_a1) rn_a
          FROM POC_SOURCE_A a) a
       FULL OUTER JOIN (SELECT NVL (x.id_x, d.id_d) id_y, NVL (x.rn_x, d.rn_d) rn_y, x.*, d.*
                          FROM (SELECT NVL (b.id_b, c.id_c) id_x, NVL (b.rn_b, c.rn_c) rn_x, b.*, c.*
                                  FROM (SELECT b.*, ROW_NUMBER () OVER (PARTITION BY id_b ORDER BY col_b1) rn_b
                                          FROM POC_SOURCE_B b) b
                                       FULL OUTER JOIN (SELECT c.*, ROW_NUMBER () OVER (PARTITION BY id_c ORDER BY col_c1) rn_c
                                                          FROM POC_SOURCE_C c) c
                                          ON b.id_b = c.id_c AND b.rn_b = c.rn_c) x
                               FULL OUTER JOIN (SELECT d.*, ROW_NUMBER () OVER (PARTITION BY id_d ORDER BY col_d1) rn_d
                                                  FROM POC_SOURCE_D d) d
                                  ON x.id_x = d.id_d AND x.rn_x = d.rn_d) y
          ON y.id_y = a.id_a AND y.rn_y = a.rn_a
ORDER BY NVL (ID_A, ID_Y), NVL (rn_a, rn_y)

编辑:

DECLARE
   TYPE line_tabtype IS TABLE OF POC_TARGET_C%ROWTYPE
      INDEX BY BINARY_INTEGER;
   target line_tabtype;  
BEGIN
   FOR c1 IN (SELECT DISTINCT id_a id
                FROM POC_SOURCE_A
              UNION
              SELECT DISTINCT id_b id
                FROM POC_SOURCE_B
              UNION
              SELECT DISTINCT id_c id
                FROM POC_SOURCE_C
              UNION
              SELECT DISTINCT id_d id
                FROM POC_SOURCE_D
              ORDER BY 1)
   LOOP
      FOR c2 IN (  SELECT a.*, ROW_NUMBER () OVER (ORDER BY col_a1) rn
                     FROM POC_SOURCE_A a
                    WHERE ID_A = c1.ID
                 ORDER BY COL_A1)
      LOOP
         target (c2.rn).ID_A := c2.ID_A;
         target (c2.rn).COL_A1 := c2.COL_A1;
         target (c2.rn).COL_A2 := c2.COL_A2;
         target (c2.rn).COL_A3 := c2.COL_A3;
         target (c2.rn).COL_A4 := c2.COL_A4;
         target (c2.rn).COL_A5 := c2.COL_A5;
      END LOOP;

      FOR c2 IN (  SELECT b.*, ROW_NUMBER () OVER (ORDER BY col_b1) rn
                     FROM POC_SOURCE_B b
                    WHERE ID_B = c1.ID
                 ORDER BY COL_B1)
      LOOP
         target (c2.rn).ID_B := c2.ID_B;
         target (c2.rn).COL_B1 := c2.COL_B1;
         target (c2.rn).COL_B2 := c2.COL_B2;
         target (c2.rn).COL_B3 := c2.COL_B3;
         target (c2.rn).COL_B4 := c2.COL_B4;
         target (c2.rn).COL_B5 := c2.COL_B5;
      END LOOP;

      FOR c2 IN (  SELECT c.*, ROW_NUMBER () OVER (ORDER BY col_c1) rn
                     FROM POC_SOURCE_C c
                    WHERE ID_C = c1.ID
                 ORDER BY COL_C1)
      LOOP
         target (c2.rn).ID_C := c2.ID_C;
         target (c2.rn).COL_C1 := c2.COL_C1;
         target (c2.rn).COL_C2 := c2.COL_C2;
      END LOOP;

      FOR c2 IN (  SELECT d.*, ROW_NUMBER () OVER (ORDER BY col_d1) rn
                     FROM POC_SOURCE_D D
                    WHERE ID_D = c1.ID
                 ORDER BY COL_D1)
      LOOP
         target (c2.rn).ID_D := c2.ID_D;
         target (c2.rn).COL_D1 := c2.COL_D1;
         target (c2.rn).COL_D2 := c2.COL_D2;
      END LOOP;

      FORALL I IN target.FIRST .. target.LAST
        INSERT INTO POC_TARGET_C VALUES target(i);

      target.delete();
   END LOOP;
END;

答案 1 :(得分:0)

insert into poc_Target_C 
select id_A, col_A1, col_A2, col_A3, col_A4, col_A5, isnull(id_B,'') as id_b, isnull(col_B1,'') as col_B1, isnull(col_B2,'') as col_B2, 
isnull(col_B3,'') as col_B3, isnull(col_B4,'') as col_B4, isnull(col_B5,'') as col_B5, isnull(id_C,'') as id_C, isnull(col_C1,'') as col_C1, 
isnull(col_C2,'') as col_C2, isnull(id_D,'') as id_D, isnull(col_D1,'') as col_D1, isnull(col_D2,'') as col_D2 
from poc_source_A left outer join poc_source_B on poc_source_A.id_A=poc_source_B.id_B left outer join 
(poc_source_C left outer join poc_source_D on poc_source_C.id_C=poc_source_D.id_d 
and poc_source_C.col_C1=('C'+substring(poc_source_D.col_D1,2,len(poc_source_D.col_D1)))) on poc_source_A.id_A=poc_source_C.id_C;

这可能会帮助您以所需的格式插入数据。