共計 1366 個字符,預計需要花費 4 分鐘才能閱讀完成。
自動寫代碼機器人,免費開通
這篇文章主要介紹 Oracle 中 listagg 函數的使用方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
listagg 函數的用法
這是一個 Oracle 的列轉行函數:LISTAGG()
先看示例代碼:
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(XXX,XXX) WITHIN GROUP( ORDER BY XXX)
用法就像聚合函數一樣,通過 Group by 語句,把每個 Group 的一個字段,拼接起來。
非常方便。
同樣是聚合函數,還有一個高級用法:
就是 over(partition by XXX)
也就是說,在你不實用 Group by 語句時候,也可以使用 LISTAGG 函數:
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
以上是“Oracle 中 listagg 函數的使用方法”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注丸趣 TV 行業資訊頻道!
向 AI 問一下細節
正文完