Oracle行專列函式Listagg

2021-06-21 02:30:26 字數 1636 閱讀 5033

oracle行專列函式listagg()

這是乙個oracle的行轉列函式:listagg()

www.2cto.com  

先看示例**:

sql**  

with temp as(  

select 'china' nation ,'guangzhou' city from dual union all  

select 'china' nation ,'shanghai' city from dual union all  

select 'china' nation ,'beijing' city from dual union all  

select 'usa' nation ,'new york' city from dual union all  

select 'usa' nation ,'bostom' city from dual union all  

select 'japan' nation ,'tokyo' city from dual   

)  select nation,listagg(city,',') within group (order by city)  

from temp  

group by nation  

這是最基礎的用法:

listagg(***,***) within group( order by ***)

用法就像聚合函式一樣,通過group by語句,把每個group的乙個字段,拼接起來。

非常方便。

同樣是聚合函式,還有乙個高階用法:

就是over(partition by ***)

也就是說,在你不實用group by語句時候,也可以使用listagg函式:

www.2cto.com  

sql**  

with temp as(  

select 500 population, 'china' nation ,'guangzhou' city from dual union all  

select 1500 population, 'china' nation ,'shanghai' city from dual union all  

select 500 population, 'china' nation ,'beijing' city from dual union all  

select 1000 population, 'usa' nation ,'new york' city from dual union all  

select 500 population, 'usa' nation ,'bostom' city from dual union all  

select 500 population, 'japan' nation ,'tokyo' city from dual   

)  select population,  

nation,  

city,  

listagg(city,',') within group (order by city) over (partition by nation) rank  

from temp  

總結:listagg()把它當作sum()函式來使用就可以了。

Oracle行專列函式Listagg

oracle行專列函式listagg 這是乙個oracle的行轉列函式 listagg www.2cto.com 先看示例 sql with temp as select china nation guangzhou city from dual union all select china nat...

普通的行專列 oracle行專列 更新中。。。

這篇文章不錯 1 select 2 3 from table 4 pivot max a max b as b,max v as c for d in xiaoming as xiaoming 其中max是函式,c是要進行迴圈的字段,in是迴圈的內容,1 nvl a,b 如果a不存在就顯示b,否則顯...

REGEXP SUBSTR函式,感覺像行專列

regexp substr函式格式如下 function regexp substr string,pattern,position,occurrence,modifier srcstr 需要進行正則處理的字串 pattern 進行匹配的正規表示式 position 起始位置,從第幾個字元開始正規表...