PostgreSQLOraclePL/SQL相容性之-AGGREGATEUSINGClause聚合函式
標籤
PostgreSQL , Oracle , PL/SQL , 聚合函式 , 自定義聚合函式
背景
Oracle的自定義聚合函式的定義方法,在建立函式是,使用AGGREGATE USING Clause關鍵詞。
AGGREGATE USING Clause
Specify AGGREGATE USING to identify this function as an aggregate function, or one that evaluates a group of rows and returns a single row. You can specify aggregate functions in the select list, HAVING clause, and ORDER BY clause.
When you specify a user-defined aggregate function in a query, you can treat it as an analytic function (one that operates on a query result set). To do so, use the OVER analytic_clause syntax available for built-in analytic functions. See “Analytic Functions” for syntax and semantics.
In the USING clause, specify the name of the implementation type of the function. The implementation type must be an object type containing the implementation of the ODCIAggregate routines. If you do not specify schema, Oracle Database assumes that the implementation type is in your own schema.
Restriction on Creating Aggregate Functions
If you specify this clause, you can specify only one input argument for the function.
自定義的聚合函式,與普通聚合函式一樣,可以用於聚合、KEEP等操作SQL中。
PostgreSQL 聚合函式用法
https://www.postgresql.org/docs/10/static/functions-aggregate.html
《PostgreSQL aggregate function 1 : General-Purpose Aggregate Functions》
《PostgreSQL aggregate function 2 : Aggregate Functions for Statistics》
《PostgreSQL aggregate function 3 : Aggregate Functions for Ordered-Set》
《PostgreSQL aggregate function 4 : Hypothetical-Set Aggregate Functions》
PostgreSQL 自定義聚合函式
1、自定義普通聚合函式:
《PostgreSQL aggregate function customize》
2、自定義並行聚合函式:
《PostgreSQL Oracle 相容性之 – 自定義並行聚合函式 PARALLEL_ENABLE AGGREGATE》
《PostgreSQL 10 自定義平行計算聚合函式的原理與實踐 – (含array_agg合併多個陣列為單個一元陣列的例子)》
3、在postgres-xc中自定義多階段分散式並行聚合函式:
《Postgres-XC customized aggregate introduction》
4、在greenplum中自定義多階段分散式並行聚合函式:
《Greenplum 最佳實踐 – 估值外掛hll的使用(以及hll分式聚合函式優化)》
語法:
https://www.postgresql.org/docs/10/static/xaggr.html
https://www.postgresql.org/docs/10/static/sql-createaggregate.html
例子
1、普通聚合函式
目標:將結果聚合,並按某個欄位輸出為有序陣列。
測試表
create table recommendation_mpt (user_id int8, app_id numeric, rating numeric);
insert into recommendation_mpt select generate_series(1,10000), generate_series(1,41), random();
聚合過程中最後一步,資料排序處理函式
create or replace function final_array_agg (i_text text) returns text[] as $$
declare
result text[];
begin
select array_agg(app_id||`_`||rating) into result from
(select split_part(i,`_`,1) as app_id,
split_part(i,`_`,2) as rating -- 按它排序
from
regexp_split_to_table(i_text,`,`) t(i)
order by 2 desc) t;
return result;
end;
$$ language plpgsql strict;
create aggregate agg_append (text) (
sfunc = textcat,
stype = text,
FINALFUNC = final_array_agg);
select agg_append(app_id||`_`||rating||`,`) from recommendation_mpt;
postgres=# select agg_append(app_id||`_`||rating||`,`) from recommendation_mpt;
-[ RECORD 1 ]-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
agg_append | {24_0.91642474103719,3_0.86293408786878,21_0.824714167509228,41_0.823069900739938,28_0.82022201269865,17_0.800656013656408,33_0.764910507481545,25_0.760074479039758,30_0.757540909573436,13_0.707890411838889,20_0.704598274547607,5_0.675859381910414,40_0.674109968356788,37_0.671832457184792,31_0.666503502987325,35_0.641303175128996,23_0.640862574335188,12_0.639161774888635,10_0.634707988705486,1_0.630520141683519,39_0.589550276752561,7_0.547058736439794,4_0.541917834896594,15_0.535650313366205,34_0.529437590856105,29_0.468865198083222,14_0.456227377057076,36_0.440769889391959,27_0.431988585740328,26_0.408387354109436,22_0.359426050912589,18_0.329283143393695,19_0.266014957334846,38_0.188361912034452,16_0.150509809609503,8_0.148780386894941,6_0.142394866328686,11_0.116577256470919,32_0.0993853402324021,2_0.00736959790810943,9_0.00227751117199659,_}
當然,這個實際上現在PG已經內建了語法來支援,上面只是演示一下自定義聚合函式。
內建ORDER BY,通過string_agg進行聚合:
postgres=# select string_agg(app_id||`_`||rating, `,` order by rating desc) from recommendation_mpt ;
-[ RECORD 1 ]-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
string_agg | 24_0.91642474103719,3_0.86293408786878,21_0.824714167509228,41_0.823069900739938,28_0.82022201269865,17_0.800656013656408,33_0.764910507481545,25_0.760074479039758,30_0.757540909573436,13_0.707890411838889,20_0.704598274547607,5_0.675859381910414,40_0.674109968356788,37_0.671832457184792,31_0.666503502987325,35_0.641303175128996,23_0.640862574335188,12_0.639161774888635,10_0.634707988705486,1_0.630520141683519,39_0.589550276752561,7_0.547058736439794,4_0.541917834896594,15_0.535650313366205,34_0.529437590856105,29_0.468865198083222,14_0.456227377057076,36_0.440769889391959,27_0.431988585740328,26_0.408387354109436,22_0.359426050912589,18_0.329283143393695,19_0.266014957334846,38_0.188361912034452,16_0.150509809609503,8_0.148780386894941,6_0.142394866328686,11_0.116577256470919,32_0.0993853402324021,2_0.00736959790810943,9_0.00227751117199659
參考
https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_5009.htm
https://www.postgresql.org/docs/10/static/xaggr.html
《PostgreSQL aggregate function customize》
《PostgreSQL Oracle 相容性之 – 自定義並行聚合函式 PARALLEL_ENABLE AGGREGATE》
《PostgreSQL 10 自定義平行計算聚合函式的原理與實踐 – (含array_agg合併多個陣列為單個一元陣列的例子)》
《Postgres-XC customized aggregate introduction》
《Greenplum 最佳實踐 – 估值外掛hll的使用(以及hll分式聚合函式優化)》
《PostgreSQL Oracle 相容性之 – PL/SQL pipelined》
相關文章
- Sql Server系列:聚合函式SQLServer函式
- sql語句之分組,聚合函式SQL函式
- PostgreSQLOracle相容性-Analysis函式之keepSQLOracle函式
- ORACLE函式介紹第三篇 著名函式之聚合函式Oracle函式
- Oracle聚合函式/分析函式Oracle函式
- ORACLE函式介紹第四篇 非著名函式之聚合函式Oracle函式
- Stream聚合函式函式
- Django:聚合函式Django函式
- 聚合函式與數字函式函式
- Spark 系列(十一)—— Spark SQL 聚合函式 AggregationsSparkSQL函式
- SQL Server 聚合函式演算法優化技巧SQLServer函式演算法優化
- Django(18)聚合函式Django函式
- MySQL 聚合函式大全MySql函式
- SQL Server資料庫————模糊查詢和聚合函式SQLServer資料庫函式
- SQL語句中聚合函式忽略NULL值的總結SQL函式Null
- Oracle PL/SQL 之 函式OracleSQL函式
- oracle 10g函式大全--聚合函式Oracle 10g函式
- Oracle OCP(04):聚合函式Oracle函式
- Oracle 聚合函式詳解Oracle函式
- oracle 自定義聚合函式Oracle函式
- ORACLE 字串聚合函式 strCatOracle字串函式
- 【函式】Oracle中聚合函式rank()使用方法函式Oracle
- Sql Server函式全解(五)之系統函式SQLServer函式
- MySQL函式大全(字串函式,數學函式,日期函式,系統級函式,聚合函式)MySql函式字串
- 【SQL 學習】函式之DECODE()SQL函式
- SQL入門之3 函式2SQL函式
- SQL入門之2 函式1SQL函式
- Python利用partial偏函式生成不同的聚合函式Python函式
- Flink SQL之Over 聚合操作SQL
- 探索MySQL高階語句(數學函式、聚合函式、字串函式、日期時間函式)MySql函式字串
- 原創:oracle聚合函式介紹Oracle函式
- mongoDB中聚合函式java處理MongoDB函式Java
- spark中的聚合函式總結Spark函式
- 留存統計 引數聚合函式函式
- SQL函式之日期函式SQL函式
- sql函式SQL函式
- 【SQL】19 SQL函式SQL函式
- oracle資料庫常用分析函式與聚合函式的用法Oracle資料庫函式