Oracle 10g wmsys.wm_concat行列轉換函式的使用:
首先讓我們來看看這個神奇的函數wm_concat(列名),該函數可以把列值以","號分隔起來,並顯示成一行,接下來上例子,看看這個神奇的函數如何應用
1、把結果按分組用逗號分割,以一行列印出來。(如果需要換其它的可以用replace函數:replace(wm_concat(name),',','|'))
select t.u_id,
wmsys.wm_concat(t.goods),
wmsys.wm_concat(t.goods || '(' || t.u_id || '斤)')
from tb_index t
group by t.u_id;
2、over(partition by t.u_id)用法:
select t.u_id,
wmsys.wm_concat(t.goods || '(' || t.u_id || '斤)') over(partition by t.u_id)
from tb_index t;
3、over(order by t.u_id)用法:
select t.u_id,
wmsys.wm_concat(t.goods || '(' || t.u_id || '斤)') over(partition by t.u_id)
from tb_index t;
4、懶人擴充用法:(大表很多欄位我需要串起來)
select 'select '|| wm_concat('t.'||column_name) || ' from TB_INDEX t' from user_tab_columns where table_name='TB_INDEX';
sys_connect_by_path(columnname, seperator) :用來構造樹路徑的,所以需要和connect by一起來用。
sys_connect_by_path 函數主要作用是可以把一個父節點下的所有子節點通過某個字元進行區分,然後串連在一個列中顯示
select t.areaid,
t.parentareaid,
t.areaname,
sys_connect_by_path(t.areaname, '-') area
from tb_index t
start with t.areaname = '中國'
connect by t.parentareaid = prior t.areaid;
listagg:11gr2還新增了一個分析函數LISTAGG,這個函數的功能實現字串的串連
create table t (id number, name varchar2(30), type varchar2(20));
insert into t
select rownum, object_name, object_type from dba_objects;
select listagg(name, ',') within group(order by id)
from t
where rownum < 10;
select type, listagg(name, ',') within group(order by id) name
from t
where type in ('DIRECTORY', 'JAVA SOURCE', 'SCHEDULE')
group by type;
select name,
listagg(name, ',') within group(order by id) over(partition by type) s_name
from t
where type in ('DIRECTORY', 'JAVA SOURCE', 'SCHEDULE');
更多Oracle相關資訊見Oracle 專題頁面 http://www.bkjia.com/topicnews.aspx?tid=12