Oracle 行列轉換函式

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.