postgresql----聚合函數

聚合函數是從一組輸入中計算出一個結果的函數。express

測試表json

test=# \d tbl_test
          Table "public.tbl_test"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 id     | integer               | 
 name   | character varying(32) | 
 sex    | character varying(1)  | 

test=# select * from tbl_test;
 id | name | sex 
----+------+-----
  1 | 張三 | m
  2 | 李四 | m
  3 | 王五 | f
(3 rows)

 

通用聚合函數數組

函數 參數類型 返回類型 描述 示例 結果
array_agg(expression) 任意非數組類型 參數類型的數組 將入參包括NULL鏈接成一個數組 select array_agg(id) from tbl_test; {1,2,3}
array_agg(expression) 任意數組類型 入參數據類型

將入參數組鏈接成更高維度的數組,輸入的數組必須是相同的維度,且不容許是空或NULL函數

select array_agg(array['b','c','a']);  {{b,c,a}}
avg(expression) smallint, int, bigint, real, double precision, numeric, or interval 整形返回numeric,浮點型返回double precision,其餘和入參類型相同 平均值 select avg(id) from tbl_test; 2.0000000000000000
bit_and(expression) smallintintbigint, or bit 和入參類型相同 全部非NULL輸入值的按位與,若是全爲NULL則返回NULL select bit_and(id) from tbl_test; 0
bit_or(expression) smallintintbigint, or bit 和入參類型相同 全部非NULL輸入值的按位或,若是全爲NULL則返回NULL select bit_or(id) from tbl_test; 3
bool_and(expression) bool bool 若是輸入全是true則返回true,不然爲false select bool_or(id::bool) from tbl_test; t
bool_or(expression) bool bool 若是輸入至少一個true,則返回true,不然返回false select bool_or((id-1)::bool) from tbl_test; t
count(*)   bigint 輸入行數 select count(*) from tbl_test; 3
count(expression) any bigint 輸入行中非NULL的行數 select count(id) from tbl_test; 3
every(expression) bool bool 功能同bool_and    
json_agg(expression) any json 將輸入聚合成一個json數組 select json_agg(id) from tbl_test; [1, 2, 3]
jsonb_agg(expression) any jsonb 將輸入聚合成一個json數組 select jsonb_agg(id) from tbl_test; [1, 2, 3]
json_object_agg(name,value) (any, any) json 將輸入組成一個key/value對的json對象 select json_object_agg('a','one'); { "a" : "one" }
jsonb_object_agg(name,value) (any, any) jsonb 將輸入組成一個key/value對的json對象 select jsonb_object_agg('a','one'); {"a": "one"}
max(expression)     輸入最大值

select max(id) from tbl_test;測試

3
min(expression)     輸入最小值 select min(id) from tbl_test; 1
string_agg(expression,delimiter) (texttext) or (byteabytea) 同參數類型 將輸入使用delimiter鏈接成一個text select string_agg(name,',') from tbl_test;  張三,李四,王五
sum(expression) smallintintbigintrealdouble precisionnumericinterval, or money   輸入和 select sum(id) from tbl_test; 6
xmlagg(expression) xml xml   請參考xml類型及其函數  

 

修改表spa

test=# alter table tbl_test add column id1 int default 1;
ALTER TABLE
test=# select * from tbl_test;
 id | name | sex | id1 
----+------+-----+-----
  1 | 張三 | m   |   1
  2 | 李四 | m   |   1
  3 | 王五 | f   |   1
(3 rows)

 

統計聚合函數code

函數 參數類型 返回類型 描述 示例 結果
corr(YX) double precision double precision 相關係數 select corr(id,id) from tbl_test; 1
covar_pop(YX) double precision double precision 整體協方差 select covar_pop(id,id) from tbl_test; 0.666666666666667
covar_samp(YX) double precision double precision 樣本協方差 select covar_samp(id,id1) from tbl_test; 0
regr_avgx(YX) double precision double precision 自變量平均值(sum(X)/N) select regr_avgx(id,id1) from tbl_test; 1
regr_avgy(YX) double precision double precision 因變量平均值(sum(Y)/N) select regr_avgy(id,id1) from tbl_test; 2
regr_count(YX) double precision bigint 兩個參數都不爲NULL的行數  select regr_count(id,id1) from tbl_test; 3
regr_intercept(YX) double precision double precision 根據全部輸入點(X,Y)利用最小二乘法計算一個線性方程式。而後返回該直線的Y軸截距 select regr_intercept(id,id) from tbl_test; 0
regr_r2(YX) double precision double precision 相關係數平方 select regr_r2(id,id) from tbl_test; 1
regr_slope(YX) double precision double precision 根據全部輸入點(X,Y)利用最小二乘法計算一個線性方程式。而後返回該直線的斜率 select regr_slope(id,id) from tbl_test; 1
regr_sxx(YX) double precision double precision sum(X^2) - sum(X)^2/N  select regr_sxx(id,id) from tbl_test; 2
regr_sxy(YX) double precision double precision sum(X*Y) - sum(X) * sum(Y)/N  select regr_sxy(id,id) from tbl_test; 2
regr_syy(YX) double precision double precision sum(Y^2) - sum(Y)^2/N  select regr_syy(id,id) from tbl_test; 2
stddev(expression)

smallint, int, bigint, real,xml

double precision, or numeric對象

double precision for floating-point arguments, blog

otherwise numeric

同stddev_samp    
stddev_pop(expression)

smallint, int, bigint, real,

double precision, or numeric

double precision for floating-point arguments,

otherwise numeric

整體標準差 select stddev_pop(id) from tbl_test; 0.81649658092772603273
stddev_samp(expression)

smallint, int, bigint, real,

double precision, or numeric

double precision for floating-point arguments,

otherwise numeric

樣本標準差 select stddev_samp(id) from tbl_test; 1.00000000000000000000
variance(expression)

smallint, int, bigint, real,

double precision, or numeric

double precision for floating-point arguments,

otherwise numeric

同var_samp    
var_pop(expression)

smallint, int, bigint, real,

double precision, or numeric

double precision for floating-point arguments,

otherwise numeric

整體方差 select var_pop(id) from tbl_test; 0.66666666666666666667
var_samp(expression)

smallint, int, bigint, real,

double precision, or numeric

double precision for floating-point arguments,

otherwise numeric

樣本方差 select var_samp(id) from tbl_test; 1.00000000000000000000
test=# insert into tbl_test values (2,'ww','f');
INSERT 0 1
test=# select * from tbl_test;
 id | name | sex | id1 
----+------+-----+-----
  1 | 張三 | m   |   1
  2 | 李四 | m   |   1
  3 | 王五 | f   |   1
  2 | ww   | f   |   1
(4 rows)

 

順序集聚合函數

函數 直接參數類型 聚合參數類型 返回類型 描述 示例 結果
mode() WITHIN GROUP (ORDER BYsort_expression)   任意可排序類型 同排序類型

返回最頻繁的輸入值(若是有

多個一樣頻繁的結果,則返回第一個)

select mode() within group (order by id) from tbl_test; 2
percentile_cont(fraction) WITHIN GROUP (ORDER BY sort_expression) double precision double precisionor interval 同排序類型 continuous percentile: returns a value corresponding to the specified fraction in the ordering, interpolating between adjacent input items if needed select percentile_cont(0.25) WITHIN GROUP (ORDER BY id) from tbl_test; 1.75
percentile_cont(fractions) WITHIN GROUP (ORDER BY sort_expression) double precision[] double precisionor interval array of sort expression's type multiple continuous percentile: returns an array of results matching the shape of the fractionsparameter, with each non-null element replaced by the value corresponding to that percentile    
percentile_disc(fraction) WITHIN GROUP (ORDER BY sort_expression) double precision any sortable type same as sort expression discrete percentile: returns the first input value whose position in the ordering equals or exceeds the specified fraction    
percentile_disc(fractions) WITHIN GROUP (ORDER BY sort_expression) double precision[] any sortable type array of sort expression's type multiple discrete percentile: returns an array of results matching the shape of the fractions parameter, with each non-null element replaced by the input value corresponding to that percentile
相關文章
相關標籤/搜索