sql行列轉置的實現方法
總結一下關於行列轉置的實現方法1、固定列數的行列轉換
如
student subject grade--------- ---------- --------student1 語文 80
student1 數學 70
student1 英語 60
student2 語文 90
student2 數學 80
student2 英語 100
……
轉換為
student 語文 數學 英語
student1 80 70 60
student2 90 80 100
……
語句如下:select student, sum(decode(subject,'語文', grade,null)) "語文",sum(decode(subject,'數學', grade,null)) "數學",sum(decode(subject,'英語', grade,null)) "英語"from tablegroup by student;[@more@]2、不定列行列轉換
如
c1 c2--- -----------1 我1 是1 誰2 知2 道3 不
……
轉換為1 我是誰2 知道3 不
這一型別的轉換可以藉助於PL/SQL來完成,這裡給一個例子CREATE OR REPLACE FUNCTION get_c2(tmp_c1 NUMBER)RETURN VARCHAR2IS
Col_c2 VARCHAR2(4000);BEGINFOR 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;
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 rnGROUP BY c1;
3、列數不固定(交叉錶行列轉置)
這種是比較麻煩的一種,需要藉助pl/sql:
原始資料:
CLASS1 CALLDATE CALLCOUNT1 2005-08-08 401 2005-08-07 62 2005-08-08 773 2005-08-09 333 2005-08-08 93 2005-08-07 21
轉置後:
CALLDATE CallCount1 CallCount2 CallCount3------------ ---------- ---------- ----------2005-08-09 0 0 332005-08-08 40 77 92005-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_getrecordIS
TYPE myrctype IS REF CURSOR;END pkg_getrecord;/
3). 建立動態sql交叉表函式,輸出結果集 CREATE OR REPLACE FUNCTION fn_rs
RETURN pkg_getrecord.myrctypeIS
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 332005-08-08 40 77 92005-08-07 6 0 21
如
student subject grade--------- ---------- --------student1 語文 80
student1 數學 70
student1 英語 60
student2 語文 90
student2 數學 80
student2 英語 100
……
轉換為
student 語文 數學 英語
student1 80 70 60
student2 90 80 100
……
語句如下:select student, sum(decode(subject,'語文', grade,null)) "語文",sum(decode(subject,'數學', grade,null)) "數學",sum(decode(subject,'英語', grade,null)) "英語"from tablegroup by student;[@more@]2、不定列行列轉換
如
c1 c2--- -----------1 我1 是1 誰2 知2 道3 不
……
轉換為1 我是誰2 知道3 不
這一型別的轉換可以藉助於PL/SQL來完成,這裡給一個例子CREATE OR REPLACE FUNCTION get_c2(tmp_c1 NUMBER)RETURN VARCHAR2IS
Col_c2 VARCHAR2(4000);BEGINFOR 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;
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 rnGROUP BY c1;
3、列數不固定(交叉錶行列轉置)
這種是比較麻煩的一種,需要藉助pl/sql:
原始資料:
CLASS1 CALLDATE CALLCOUNT1 2005-08-08 401 2005-08-07 62 2005-08-08 773 2005-08-09 333 2005-08-08 93 2005-08-07 21
轉置後:
CALLDATE CallCount1 CallCount2 CallCount3------------ ---------- ---------- ----------2005-08-09 0 0 332005-08-08 40 77 92005-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_getrecordIS
TYPE myrctype IS REF CURSOR;END pkg_getrecord;/
3). 建立動態sql交叉表函式,輸出結果集 CREATE OR REPLACE FUNCTION fn_rs
RETURN pkg_getrecord.myrctypeIS
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 332005-08-08 40 77 92005-08-07 6 0 21
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/787895/viewspace-1000345/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL 如何實現動態的行列轉置SQL
- sql 實現表的行列轉換SQL
- 一個sql的行列轉置的例子SQL
- 【SQL】行列轉換方法示例SQL
- 試驗Oracle中實現行列轉換的方法(轉)Oracle
- SQL Server 實現行列(縱橫表)轉換SQLServer
- 使用動態SQL語句實現簡單的行列轉置(動態產生列)SQL
- 使用vue實現行列轉換的一種方法。Vue
- WORD及EXCEL行列轉換實現方法收藏(轉)Excel
- sql語句實現表的行列倒置SQL
- 在SQL Server 2005中實現表的行列轉換SQLServer
- 行列轉換sqlSQL
- Oracle--SQL行列轉換實戰OracleSQL
- sql server 行列轉換SQLServer
- 通用的行列轉換的方法
- pivot、unpivot實現oracle行列轉換Oracle
- sqlserver 行列互轉實現小結SQLServer
- sql server行列轉換案例SQLServer
- 【SQL 學習】行列轉換SQL
- Spark實現行列轉換pivot和unpivotSpark
- 在Word中實現表格的行列互換 (轉)
- 用abap實現內表的行列轉換-原始碼2 (轉)原始碼
- 用ORACLE分析函式實現行列轉換Oracle函式
- SQL Server中行列轉換 Pivot UnPivotSQLServer
- excel列轉行怎麼做 excel如何轉置行列Excel
- wmsys.wm_concat 實現行列轉換問題
- SQL Server 2005下的行列轉化(簡單)SQLServer
- SQL Server 2005之PIVOT/UNPIVOT行列轉換(轉)SQLServer
- 警示:通過 wmsys.wm_concat 實現行列轉換
- 實現累加的經典sql方法SQL
- 幾個行列轉換的實用小例子
- SQL 轉置計算SQL
- 【SQL 分析函式】wm_concat 行列轉換SQL函式
- 行列轉換
- 實現二維陣列的行列互換陣列
- sql實現行轉列SQL
- 9*9乘法口決pl/sql的多種方法實現(pl/sql實現)SQL
- 複雜的行列轉換