行列转换

1、 固定列数的行列转换 如
  
 name subject grade
    ---------------------------
    student1 语文 80						      转换为                   
    student1 数学 70						       语文 数学 英语       
    student1 英语 60						       student1 80 70 60 
    student2 语文 90						       student2 90 80 100
    student2 数学 80
    student2 英语 100
  按照name分组得到的结果,所以decode外面要套个sum,不然group by出现语义错误,结果为
   
SELECT NAME,
           SUM(DECODE(SUBJECT, '语文', GRADE, NULL)) "语文",
           SUM(DECODE(SUBJECT, '数学', GRADE, NULL)) "数学",
           SUM(DECODE(SUBJECT, '英语', GRADE, NULL)) "英语"
      FROM STUDENT
     GROUP BY NAME
    2、 不定列行列转换
   如
    c1 c2
    --------------
    1 我
    1 是   转换为        
    1 谁 1 我是谁     
    2 知 2 知道        
    2 道 3 不           
    3 不
   
    这一类型的转换必须借助于PL/SQL 来完成,这里给一个例子
    
    CREATE OR REPLACE FUNCTION GET_C2(TMP_C1 NUMBER) RETURN VARCHAR2 IS
      COL_C2 VARCHAR2(4000);
    BEGIN
      FOR CUR IN (SELECT C2 FROM T WHERE C1 = TMP_C1) LOOP
        COL_C2 := COL_C2 || CUR.C2;
      END LOOP;
      COL_C2 := RTRIM(COL_C2, 1);
      RETURN COL_C2;
    END;
    /
 
  SQL> select distinct c1 ,get_c2(c1) cc2 from table;即可
    或者不用pl/sql,利用分析函数和 CONNECT_BY 实现:
    SELECT C1, SUBSTR(MAX(SYS_CONNECT_BY_PATH(C2, ';')), 2) NAME
      FROM (SELECT C1,
                   C2,
                   RN,
                   LEAD(RN) OVER(PARTITION BY C1 ORDER BY RN) RN1
              FROM (SELECT C1, C2, ROW_NUMBER() OVER(ORDER BY C2) RN FROM T))
     START WITH RN1 IS NULL
    CONNECT BY RN1 = PRIOR RN
     GROUP BY C1;
    3、列数不固定(交叉表行列转置)
    这种是比较麻烦的一种,需要借助pl/sql:
    原始数据:
 
  CLASS1 CALLDATE CALLCOUNT
    1 2005-08-08 40
    1 2005-08-07 6
    2 2005-08-08 77
    3 2005-08-09 33
    3 2005-08-08 9
    3 2005-08-07 21
   转置后:
    CALLDATE CallCount1 CallCount2 CallCount3
    ------------ ----------    ----------	----------
    2005-08-09	0		 0		 33
    2005-08-08	40		77		 9
    2005-08-07	6		0		21
   试验如下:
    1). 建立测试表和数据
引用
    CREATE TABLE t(
    class1 VARCHAR2(2 BYTE),
    calldate DATE,
    callcount INTEGER
    );
    INSERT INTO t(class1, calldate, callcount)
    VALUES ('1', TO_DATE ('08/08/2005', 'MM/DD/YYYY'), 40);
    INSERT INTO t(class1, calldate, callcount)
    VALUES ('1', TO_DATE ('08/07/2005', 'MM/DD/YYYY'), 6);
    INSERT INTO t(class1, calldate, callcount)
    VALUES ('2', TO_DATE ('08/08/2005', 'MM/DD/YYYY'), 77);
    INSERT INTO t(class1, calldate, callcount)
    VALUES ('3', TO_DATE ('08/09/2005', 'MM/DD/YYYY'), 33);
    INSERT INTO t(class1, calldate, callcount)
    VALUES ('3', TO_DATE ('08/08/2005', 'MM/DD/YYYY'), 9);
    INSERT INTO t(class1, calldate, callcount)
    VALUES ('3', TO_DATE ('08/07/2005', 'MM/DD/YYYY'), 21);
    COMMIT ;

    2) 建立ref cursor准备输出结果集
    CREATE OR REPLACE PACKAGE PKG_GETRECORD IS
      TYPE MYRCTYPE IS REF CURSOR;
    END PKG_GETRECORD;
    /
   3). 建立动态sql交叉表函数,输出结果集
    CREATE OR REPLACE FUNCTION FN_RS RETURN PKG_GETRECORD.MYRCTYPE IS
    S VARCHAR2(4000);
    CURSOR C1 IS
      SELECT ',sum(case when Class1=' || CLASS1 ||
             ' then CallCount else 0 end)' || ' "CallCount' || CLASS1 || '"' C2
        FROM T
       GROUP BY CLASS1;
    R1          C1%ROWTYPE;
    LIST_CURSOR PKG_GETRECORD.MYRCTYPE;
  BEGIN
    S := 'select CallDate ';
    OPEN C1;
    LOOP
      FETCH C1
        INTO R1;
      EXIT WHEN C1%NOTFOUND;
      S := S || R1.C2;
    END LOOP;
    CLOSE C1;
    S := S || ' from T group by CallDate order by CallDate desc ';
    OPEN LIST_CURSOR FOR S;
    RETURN LIST_CURSOR;
  END FN_RS;
  /
    4). 测试在sql plus下执行:
   var results refcursor;
    exec :results := fn_rs;
    print results;
    CALLDATE	CallCount1 CallCount2 CallCount3
    --------------- ----------	 ----------	  ----------
    2005-08-09		0		0		33
    2005-08-08		40		77		 9
    2005-08-07		6		0		21
   
   4、 不分组只简单行列互换,并且每组之间用虚线分割,可以使用procedure,代码如下:
    CREATE OR REPLACE PROCEDURE PRINT_TABLE(P_QUERY IN VARCHAR2) AUTHID CURRENT_USER AS
  L_THECURSOR   INTEGER DEFAULT DBMS_SQL.OPEN_CURSOR;
  L_COLUMNVALUE VARCHAR2(4000);
  L_STATUS      INTEGER;
  L_DESCTBL     DBMS_SQL.DESC_TAB;
  L_COLCNT      NUMBER;
BEGIN
  DBMS_SQL.PARSE(L_THECURSOR, P_QUERY, DBMS_SQL.NATIVE);
  DBMS_SQL.DESCRIBE_COLUMNS(L_THECURSOR, L_COLCNT, L_DESCTBL);
  FOR I IN 1 .. L_COLCNT LOOP
    DBMS_SQL.DEFINE_COLUMN(L_THECURSOR, I, L_COLUMNVALUE, 4000);
  END LOOP;
  L_STATUS := DBMS_SQL.EXECUTE(L_THECURSOR);
  WHILE (DBMS_SQL.FETCH_ROWS(L_THECURSOR) > 0) LOOP
    FOR I IN 1 .. L_COLCNT LOOP
      DBMS_SQL.COLUMN_VALUE(L_THECURSOR, I, L_COLUMNVALUE);
      DBMS_OUTPUT.PUT_LINE(RPAD(L_DESCTBL(I).COL_NAME, 30) || L_C
                           OLUMNVALUE);
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('---------------------------------------------');
  END LOOP;
EXCEPTION
  WHEN OTHERS THEN
    DBMS_SQL.CLOSE_CURSOR(L_THECURSOR);
    RAISE;
    NULL;
END;

你可能感兴趣的:(行列转换)