Oracle 行轉列、列轉行 的Sql語句總結,oraclesql

來源:互聯網
上載者:User

Oracle 行轉列、列轉行 的Sql語句總結,oraclesql

參考文章:http://blog.csdn.net/tianlesoftware/article/details/4704858多行轉字串這個比較簡單,用||或concat函數可以實現
select concat(id,username) str from app_userselect id||username str from app_user
字串轉多列實際上就是拆分字串的問題,可以使用 substr、instr、regexp_substr函數方式字串轉多行使用union all函數等方式
wm_concat函數

首先讓我們來看看這個神奇的函數wm_concat(列名),該函數可以把列值以","號分隔起來,並顯示成一行,接下來上例子,看看這個神奇的函數如何應用準備測試資料

create table test(id number,name varchar2(20));insert into test values(1,'a');insert into test values(1,'b');insert into test values(1,'c');insert into test values(2,'d');insert into test values(2,'e');

效果1 :   行轉列  ,預設逗號隔開

select wm_concat(name) name from test;

效果2:   把結果裡的逗號替換成"|"

select replace(wm_concat(name),',','|') from test;

效果3:  按ID分組合并name
select id,wm_concat(name) name from test group by id;

sql語句等同於下面的sql語句
-------- 適用範圍:8i,9i,10g及以後版本  ( MAX + DECODE )select id, max(decode(rn, 1, name, null)) || max(decode(rn, 2, ',' || name, null)) || max(decode(rn, 3, ',' || name, null)) str       from (select id,name,row_number() over(partition by id order by name) as rn from test) t group by id order by 1;      -------- 適用範圍:8i,9i,10g及以後版本 ( ROW_NUMBER + LEAD )select id, str from (select id,row_number() over(partition by id order by name) as rn,name || lead(',' || name, 1) over(partition by id order by name) ||       lead(',' || name, 2) over(partition by id order by name) || lead(',' || name, 3) over(partition by id order by name) as str from test) where rn = 1 order by 1;  -------- 適用範圍:10g及以後版本 ( MODEL )select id, substr(str, 2) str from test model return updated rows partition by(id) dimension by(row_number() over(partition by id order by name) as rn) measures (cast(name as varchar2(20)) as str) rules upsert iterate(3) until(presentv(str[iteration_number+2],1,0)=0) (str[0] = str[0] || ',' || str[iteration_number+1]) order by 1;              -------- 適用範圍:8i,9i,10g及以後版本 ( MAX + DECODE )select t.id id,max(substr(sys_connect_by_path(t.name,','),2)) str from (select id, name, row_number() over(partition by id order by name) rn from test) t       start with rn = 1 connect by rn = prior rn + 1 and id = prior id group by t.id;</span>
懶人擴充用法: 案例: 我要寫一個視圖,類似"create or replace view as select 欄位1,...欄位50 from tablename" ,基表有50多個欄位,要是靠手工寫太麻煩了,有沒有什麼簡便的方法? 當然有了,看我如果應用wm_concat來讓這個需求變簡單,假設我的APP_USER表中有(id,username,password,age)4個欄位。查詢結果如下

 /** 這裡的表名預設區分大小寫 */ select 'create or replace view as select '|| wm_concat(column_name) || ' from APP_USER' sqlStr from user_tab_columns where table_name='APP_USER';

利用系統資料表方式查詢

select * from user_tab_columns


Oracle 11g 行列互換 pivot 和 unpivot 說明

在Oracle 11g中,Oracle 又增加了2個查詢:pivot(行轉列) 和unpivot(列轉行)

參考:http://blog.csdn.net/tianlesoftware/article/details/7060306、http://www.oracle.com/technetwork/cn/articles/11g-pivot-101924-zhs.html

google 一下,網上有一篇比較詳細的文檔:http://www.oracle-developer.net/display.php?id=506

pivot 列轉行測試資料 (id,類型名稱,銷售數量),案例:根據水果的類型查詢出一條資料顯示出每種類型的銷售數量。

create table demo(id int,name varchar(20),nums int);  ---- 建立表insert into demo values(1, '蘋果', 1000);insert into demo values(2, '蘋果', 2000);insert into demo values(3, '蘋果', 4000);insert into demo values(4, '橘子', 5000);insert into demo values(5, '橘子', 3000);insert into demo values(6, '葡萄', 3500);insert into demo values(7, '芒果', 4200);insert into demo values(8, '芒果', 5500);

分組查詢 (當然這是不符合查詢一條資料的要求的)

select name, sum(nums) nums from demo group by name


行轉列查詢

select * from (select name, nums from demo) pivot (sum(nums) for name in ('蘋果' 蘋果, '橘子', '葡萄', '芒果'));

注意: pivot(彙總函式 for 列名 in(類型))   ,其中 in(‘’) 中可以指定別名

當然也可以不使用pivot函數,等同於下列語句,只是代碼比較長,容易理解

select * from (select sum(nums) 蘋果 from demo where name='蘋果'),(select sum(nums) 橘子 from demo where name='橘子'),       (select sum(nums) 葡萄 from demo where name='葡萄'),(select sum(nums) 芒果 from demo where name='芒果') ;       
unpivot 行轉列顧名思義就是將多列轉換成1列中去
案例:現在有一個水果表,記錄了4個季度的銷售數量,現在要將每種水果的每個季度的銷售情況用多行資料展示。

建立表和資料

create table Fruit(id int,name varchar(20), Q1 int, Q2 int, Q3 int, Q4 int);insert into Fruit values(1,'蘋果',1000,2000,3300,5000);insert into Fruit values(2,'橘子',3000,3000,3200,1500);insert into Fruit values(3,'香蕉',2500,3500,2200,2500);insert into Fruit values(4,'葡萄',1500,2500,1200,3500);select * from Fruit

列轉行查詢

select id , name, jidu, xiaoshou from Fruit unpivot (xiaoshou for jidu in (q1, q2, q3, q4) )
注意:  unpivot沒有彙總函式,xiaoshou、jidu欄位也是臨時的變數

同樣不使用unpivot也可以實現同樣的效果,只是sql語句會很長,而且執行速度效率也沒有前者高

select id, name ,'Q1' jidu, (select q1 from fruit where id=f.id) xiaoshou from Fruit funionselect id, name ,'Q2' jidu, (select q2 from fruit where id=f.id) xiaoshou from Fruit funionselect id, name ,'Q3' jidu, (select q3 from fruit where id=f.id) xiaoshou from Fruit funionselect id, name ,'Q4' jidu, (select q4 from fruit where id=f.id) xiaoshou from Fruit f

XML類型上述pivot列轉行樣本中,你已經知道了需要查詢的類型有哪些,用in()的方式包含,假設如果您不知道都有哪些值,您怎麼構建查詢呢?








相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.