日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Oracle中的within,Oracle函数 --聚合函数中的语法within group

發布時間:2025/3/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Oracle中的within,Oracle函数 --聚合函数中的语法within group 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Oracle的聚合函數一般與group by 聯合使用,但一般通過group by 聚合

但某些聚合函數會后跟

WITHIN GROUP

(ORDER BY

expr [ DESC | ASC ]

[ NULLS { FIRST | LAST } ]

[, expr [ DESC | ASC ]

[ NULLS { FIRST | LAST } ]

]...

)

) 指定在group 組內的順序。

可理解為 在組內的順序,日后也許會有其他應用。

For example 1 :

同組字符串拼接函數(11gR2新增, 與10g中?WMSYS.WM_CONCAT相同)

LISTAGG(measure_expr [, 'delimiter'])

WITHIN GROUP (order_by_clause) [OVER query_partition_clause]

(注: over()為可選語法,在次函數中,與group by 作用相同,即 group by () 與over()選一即可)

例一:合并同組的字符,連接方式以city倒敘排序

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 desc)

from temp

group by nation;

例二:使用over()代替group by 語法

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,

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

from temp;

For example 2:

Rank() 函數--rank函數有聚合函數也可使用分析函數(二者不可同時使用)

--分析函數為選定的字段按指定順序表名排序,可與其他字段一同出現;

--聚合函數將指定參數的在指定排序的結果集中可排順序, 函數只可單獨使用, 指定

例一: 使用rank查詢參數在結果集排序中得位置(注:rank中得參數必須與within group(order by)中指定的參數相同(數量,順序))

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

rank(500,'Bf') within GROUP (order by population,city desc) rank --Bf僅為一個參數,結果為Bf在population=500中可以排第幾

from temp;

--參考表

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

from temp

order by population,city desc

總結

以上是生活随笔為你收集整理的Oracle中的within,Oracle函数 --聚合函数中的语法within group的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。