例如這種資料
1 a
1 b
1 c
1 d
1 e
排成
1 a b c d e 這樣的形式
uj5u.com熱心網友回復:
是不是有專門的函式uj5u.com熱心網友回復:
樓主百度一下 “行轉列” ,應該有很多示例,有其他問題再來追問;uj5u.com熱心網友回復:
wm_concat函式uj5u.com熱心網友回復:
PL/SQL 是用不了wm_concat函式嗎uj5u.com熱心網友回復:
這個要看版本,一般都能使用。
uj5u.com熱心網友回復:
你試試看:select id,replace(wm_concat(str)) from table_name group by id
uj5u.com熱心網友回復:
這里假設第一列為id,第二列為struj5u.com熱心網友回復:
用得了啊,我現在還會使用到。只是資料庫版本比較高的話用listagg好點
uj5u.com熱心網友回復:
我試了一下,oracle 11g是沒有這個函式的
select id,replace(wm_concat(value)) from test group by id
*
第 1 行出現錯誤:
ORA-00938: 函式沒有足夠的引數
uj5u.com熱心網友回復:
如果你想問的是如何行轉列的話那么就是如下這樣的陳述句SQL> select * from test;
ID VALUE
---------- ----------------
1 a
1 b
1 c
1 d
1 e
select id,max(case value when 'a' then value else '' end) word1,
max(case value when 'b' then value else '' end) word2,
max(case value when 'c' then value else '' end) word3,
max(case value when 'd' then value else '' end) word4,
max(case value when 'e' then value else '' end) word5
from test
group by id
/
ID WORD1 WORD2 WORD3 WORD4 WORD5
----- ---------- ---------- ---------- ---------- ----------
1 a b c d e
有一個更好的例子:
SQL> select * from t4;
ID NAME SUBJEC GRADE
---------- ---------- ------ -----
1 ZORRO 語文 70
2 ZORRO 數學 80
3 ZORRO 英語 75
4 SEKER 語文 65
5 SEKER 數學 75
6 SEKER 英語 60
7 BLUES 語文 60
8 BLUES 數學 90
9 PG 數學 80
10 PG 英語 90
select name,sum(case subject when '語文' then grade else 0 end) 語文,
sum(case subject when '數學' then grade else 0 end) 數學,
sum(case subject when '英語' then grade else 0 end) 英語
from t4
group by name;
NAME 語文 數學 英語
---------- ---------- ---------- ----------
SEKER 65 75 60
BLUES 60 90 0
PG 0 80 90
ZORRO 70 80 75
我感覺這個能更好的理解行轉列問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/96382.html
標籤:基礎和管理
上一篇:ORACLE.exe 卡硬碟
下一篇:plsql中有一個用戶db_t1,如何讓其他的非DBA用戶無法訪問db_t1下的表,只有DBA權限的用戶可以訪問?
