聚集函数
聚集函数
- sum(expression)
返回类型:
通常情况下输入数据类型和输出数据类型是相同的,但以下情况会发生类型转换:
- 对于SMALLINT或INT输入,输出类型为BIGINT。
- 对于BIGINT输入,输出类型为NUMBER 。
- 对于浮点数输入,输出类型为DOUBLE PRECISION。
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE tab(a int); CREATE TABLE openGauss=# INSERT INTO tab values(1); INSERT 0 1 openGauss=# INSERT INTO tab values(2); INSERT 0 1 openGauss=# SELECT sum(a) FROM tab; sum ----- 3 (1 row)
- max(expression)
参数类型:任意数组、数值、字符串、日期/时间类型。
返回类型:与参数数据类型相同
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE max_t1(a int, b int); openGauss=# INSERT INTO max_t1 VALUES(1,2),(2,3),(3,4),(4,5); openGauss=# SELECT MAX(a) FROM max_t1; max ----- 4 (1 row) openGauss=# DROP TABLE max_t1;
- min(expression)
参数类型:任意数组、数值、字符串、日期/时间类型。
返回类型:与参数数据类型相同
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE min_t1(a int, b int); openGauss=# INSERT INTO min_t1 VALUES(1,2),(2,3),(3,4),(4,5); openGauss=# SELECT MIN(a) FROM min_t1; min ----- 1 (1 row) openGauss=# DROP TABLE min_t1;
- avg(expression)
返回类型:
对于任何整数类型输入,结果都是NUMBER类型。
对于任何浮点输入,结果都是DOUBLE PRECISION类型。
否则和输入数据类型相同。
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE avg_t1(a int, b int); openGauss=# INSERT INTO avg_t1 VALUES(1,2),(2,3),(3,4),(4,5); openGauss=# SELECT AVG(a) FROM avg_t1; avg -------------------- 2.5000000000000000 (1 row) openGauss=# DROP TABLE avg_t1;
- count(expression)
返回类型:BIGINT
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE count_t1(a int, b int); openGauss=# INSERT INTO count_t1 VALUES (NULL,1),(1,2),(2,3),(3,4),(4,5); openGauss=# SELECT COUNT(a) FROM count_t1; count ------- 4 (1 row) openGauss=# DROP TABLE count_t1;
- count(*)
返回类型:BIGINT
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE count_t1(a int, b int); openGauss=# INSERT INTO count_t1 VALUES (NULL,1),(1,2),(2,3),(3,4),(4,5); openGauss=# SELECT COUNT(*) FROM count_t1; count ------- 5 (1 row) openGauss=# DROP TABLE count_t1;
- median(expression) [over (query partition clause)]
描述:返回表达式的中位数,计算时NULL将会被median函数忽略。可以使用distinct关键字排除表达式中的重复记录。输入expression的数据类型可以是数值类型(包括integer, double,bigint等), 也可以是interval类型。其他数据类型不支持求取中位数。
返回类型:double或interval类型
示例:
SELECT median(id) FROM (values(1), (2), (3), (4), (null)) test(id); median -------- 2.5 (1 row)
- array_agg(expression)
返回类型:参数类型的数组
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE array_agg_t1(a int, b int); openGauss=# INSERT INTO array_agg_t1 VALUES (NULL,1),(1,2),(2,3),(3,4),(4,5); openGauss=# SELECT ARRAY_AGG(a) FROM array_agg_t1; array_agg ---------------- {NULL,1,2,3,4} (1 row) openGauss=# DROP TABLE array_agg_t1;
- string_agg(expression, delimiter)
返回类型:和参数数据类型相同。
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE string_agg_t1(a int, b int); openGauss=# INSERT INTO string_agg_t1 VALUES (NULL,1),(1,2),(2,3),(3,4),(4,5); openGauss=# SELECT STRING_AGG(a,';') FROM string_agg_t1; string_agg ------------ 1;2;3;4 (1 row) openGauss=# DROP TABLE string_agg_t1;
- listagg(expression [, delimiter]) WITHIN GROUP(ORDER BY order-list)
描述:将聚集列数据按WITHIN GROUP指定的排序方式排列,并用delimiter指定的分隔符拼接成一个字符串。
- expression:必选。指定聚集列名或基于列的有效表达式,不支持DISTINCT关键字和VARIADIC参数。
- delimiter:可选。指定分隔符,可以是字符串常数或基于分组列的确定性表达式,缺省时表示分隔符为空。
- order-list:必选。指定分组内的排序方式。
返回类型:text
示例:
聚集列是文本字符集类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
openGauss=# CREATE TABLE listagg_t1(a int, b text); openGauss=# INSERT INTO listagg_t1 VALUES (NULL,'a1'),(1,'b2'),(1,'c3'),(2,'d4'),(2,'e5'),(3,'f6'); openGauss=# SELECT a,LISTAGG(b,';') WITHIN GROUP(ORDER BY b) FROM listagg_t1 group by a; a | listagg ---+--------- 1 | b2;c3 2 | d4;e5 3 | f6 | a1 (4 rows) openGauss=# DROP TABLE listagg_t1;
聚集列是整型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
openGauss=# CREATE TABLE listagg_t1(a int, b int); openGauss=# INSERT INTO listagg_t1 VALUES (NULL,1),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT a,LISTAGG(b,';') WITHIN GROUP(ORDER BY b) FROM listagg_t1 group by a; a | listagg ---+--------- 1 | 2;3 2 | 4;5 3 | 6 | 1 (4 rows) openGauss=# DROP TABLE listagg_t1;
聚集列是浮点类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
openGauss=# CREATE TABLE listagg_t1(a int, b float); openGauss=# INSERT INTO listagg_t1 VALUES (NULL,1.111),(1,2.222),(1,3.333),(2,4.444),(2,5.555),(3,6.666); openGauss=# SELECT a,LISTAGG(b,';') WITHIN GROUP(ORDER BY b) FROM listagg_t1 group by a; a | listagg ---+------------------- 1 | 2.222000;3.333000 2 | 4.444000;5.555000 3 | 6.666000 | 1.111000 (4 rows) openGauss=# DROP TABLE listagg_t1;
聚集列是时间类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
openGauss=# CREATE TABLE listagg_t1(a int, b timestamp); openGauss=# INSERT INTO listagg_t1 VALUES (NULL,'2000-01-01'),(1,'2000-02-02'),(1,'2000-03-03'),(2,'2000-04-04'),(2,'2000-05-05'),(3,'2000-06-06'); openGauss=# SELECT a,LISTAGG(b,';') WITHIN GROUP(ORDER BY b) FROM listagg_t1 group by a; a | listagg ---+----------------------------------------- 1 | 2000-02-02 00:00:00;2000-03-03 00:00:00 2 | 2000-04-04 00:00:00;2000-05-05 00:00:00 3 | 2000-06-06 00:00:00 | 2000-01-01 00:00:00 (4 rows) openGauss=# DROP TABLE listagg_t1;
聚集列是时间间隔类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
openGauss=# CREATE TABLE listagg_t1(a int, b interval); openGauss=# INSERT INTO listagg_t1 VALUES (NULL,'1 days'),(1,'2 days'),(1,'3 days'),(2,'4 days'),(2,'5 days'),(3,'6 days'); openGauss=# SELECT a,LISTAGG(b,';') WITHIN GROUP(ORDER BY b) FROM listagg_t1 group by a; a | listagg ---+--------------- 1 | 2 days;3 days 2 | 4 days;5 days 3 | 6 days | 1 day (4 rows) openGauss=# DROP TABLE listagg_t1;
分隔符缺省时,默认为空。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
openGauss=# CREATE TABLE listagg_t1(a int, b interval); openGauss=# INSERT INTO listagg_t1 VALUES (NULL,'1 days'),(1,'2 days'),(1,'3 days'),(2,'4 days'),(2,'5 days'),(3,'6 days'); openGauss=# SELECT a,LISTAGG(b) WITHIN GROUP(ORDER BY b) FROM listagg_t1 group by a; a | listagg ---+-------------- 1 | 2 days3 days 2 | 4 days5 days 3 | 6 days | 1 day (4 rows) openGauss=# DROP TABLE listagg_t1;
listagg作为窗口函数时,OVER子句不支持ORDER BY的窗口排序,listagg列为对应分组的有序聚集。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
openGauss=# CREATE TABLE listagg_t1(a int, b interval); openGauss=# INSERT INTO listagg_t1 VALUES (NULL,'1 days'),(1,'2 days'),(1,'3 days'),(2,'4 days'),(2,'5 days'),(3,'6 days'); openGauss=# SELECT a,LISTAGG(b) WITHIN GROUP(ORDER BY b) OVER(PARTITION BY a) FROM listagg_t1; a | listagg ---+-------------- 1 | 2 days3 days 1 | 2 days3 days 2 | 4 days5 days 2 | 4 days5 days 3 | 6 days | 1 day (6 rows) openGauss=# DROP TABLE listagg_t1;
- covar_pop(Y, X)
返回类型:double precision
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE covar_pop_t1(a int, b int); openGauss=# INSERT INTO covar_pop_t1 VALUES (NULL,11),(11,21),(11,31),(21,41),(21,51),(31,61); openGauss=# SELECT COVAR_POP(a,b) FROM covar_pop_t1; covar_pop ----------- 100 (1 row) openGauss=# DROP TABLE covar_pop_t1;
- covar_samp(Y, X)
返回类型:double precision
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE covar_samp_t1(a int, b int); openGauss=# INSERT INTO covar_samp_t1 VALUES (NULL,11),(11,21),(11,31),(21,41),(21,51),(31,61); openGauss=# SELECT COVAR_SAMP(a,b) FROM covar_samp_t1; covar_samp ------------ 125 (1 row) openGauss=# DROP TABLE covar_samp_t1;
- stddev_pop(expression)
返回类型:对于浮点类型的输入返回double precision,其他输入返回numeric。
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE stddev_pop_t1(a int, b int); openGauss=# INSERT INTO stddev_pop_t1 VALUES (NULL,11),(11,21),(11,31),(21,41),(21,51),(31,61); openGauss=# SELECT STDDEV_POP(a) FROM stddev_pop_t1; stddev_pop -------------------- 7.4833147735478828 (1 row) openGauss=# DROP TABLE stddev_pop_t1;
- stddev_samp(expression)
返回类型:对于浮点类型的输入返回double precision,其他输入返回numeric。
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE stddev_samp_t1(a int, b int); openGauss=# INSERT INTO stddev_samp_t1 VALUES (NULL,11),(11,21),(11,31),(21,41),(21,51),(31,61); openGauss=# SELECT STDDEV_SAMP(a) FROM stddev_samp_t1; stddev_samp -------------------- 8.3666002653407555 (1 row) openGauss=# DROP TABLE stddev_samp_t1;
- var_pop(expression)
返回类型:对于浮点类型的输入返回double precision类型,其他输入返回numeric类型。
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE var_pop_t1(a int, b int); openGauss=# INSERT INTO var_pop_t1 VALUES (NULL,11),(11,21),(11,31),(21,41),(21,51),(31,61); openGauss=# SELECT VAR_POP(a) FROM var_pop_t1; var_pop --------------------- 56.0000000000000000 (1 row) openGauss=# DROP TABLE var_pop_t1;
- var_samp(expression)
返回类型:对于浮点类型的输入返回double precision类型,其他输入返回numeric类型。
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE var_samp_t1(a int, b int); openGauss=# INSERT INTO var_samp_t1 VALUES (NULL,11),(11,21),(11,31),(21,41),(21,51),(31,61); openGauss=# SELECT VAR_SAMP(a) FROM var_samp_t1; var_samp --------------------- 70.0000000000000000 (1 row) openGauss=# DROP TABLE var_samp_t1;
- bit_and(expression)
描述:所有非NULL输入值的按位与(AND),如果全部输入值皆为NULL,那么结果也为NULL 。
返回类型:和参数数据类型相同。
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE bit_and_t1(a int, b int); openGauss=# INSERT INTO bit_and_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT BIT_AND(a) FROM bit_and_t1; bit_and --------- 0 (1 row) openGauss=# DROP TABLE bit_and_t1;
- bit_or(expression)
描述:所有非NULL输入值的按位或(OR),如果全部输入值皆为NULL,那么结果也为NULL。
返回类型:和参数数据类型相同
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE bit_or_t1(a int, b int); openGauss=# INSERT INTO bit_or_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT BIT_OR(a) FROM bit_or_t1; bit_or -------- 3 (1 row) openGauss=# DROP TABLE bit_or_t1;
- bool_and(expression)
返回类型:bool
示例:
1 2 3 4 5
openGauss=# SELECT bool_and(100 <2500); bool_and ---------- t (1 row)
- bool_or(expression)
返回类型:bool
示例:
1 2 3 4 5
openGauss=# SELECT bool_or(100 <2500); bool_or ---------- t (1 row)
- corr(Y, X)
返回类型:double precision
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE corr_t1(a int, b int); openGauss=# INSERT INTO corr_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT CORR(a,b) FROM corr_t1; corr ------------------ .944911182523068 (1 row) openGauss=# DROP TABLE corr_t1;
- every(expression)
返回类型:bool
示例:
1 2 3 4 5
openGauss=# SELECT every(100 <2500); every ------- t (1 row)
- regr_avgx(Y, X)
返回类型:double precision
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE regr_t1(a int, b int); openGauss=# INSERT INTO regr_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT REGR_AVGX(a,b) FROM regr_t1; regr_avgx ----------- 4 (1 row) openGauss=# DROP TABLE regr_t1;
- regr_avgy(Y, X)
返回类型:double precision
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE regr_avgy_t1(a int, b int); openGauss=# INSERT INTO regr_avgy_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT REGR_AVGY(a,b) FROM regr_avgy_t1; regr_avgy ----------- 1.8 (1 row) openGauss=# DROP TABLE regr_avgy_t1;
- regr_count(Y, X)
返回类型:bigint
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE regr_count_t1(a int, b int); openGauss=# INSERT INTO regr_count_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT REGR_COUNT(a,b) FROM regr_count_t1; regr_count ------------ 5 (1 row) openGauss=# DROP TABLE regr_count_t1;
- regr_intercept(Y, X)
描述:根据所有输入的点(X, Y)按照最小二乘法拟合成一个线性方程,然后返回该直线的Y轴截距。
返回类型:double precision
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE regr_intercept_t1(a int, b int); openGauss=# INSERT INTO regr_intercept_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT REGR_INTERCEPT(b,a) FROM regr_intercept_t1; regr_intercept ------------------ .785714285714286 (1 row) openGauss=# DROP TABLE regr_intercept_t1;
- regr_r2(Y, X)
返回类型:double precision
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE regr_r2_t1(a int, b int); openGauss=# INSERT INTO regr_r2_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT REGR_R2(b,a) FROM regr_r2_t1; regr_r2 ------------------ .892857142857143 (1 row) openGauss=# DROP TABLE regr_r2_t1;
- regr_slope(Y, X)
描述:根据所有输入的点(X, Y)按照最小二乘法拟合成一个线性方程, 然后返回该直线的斜率。
返回类型:double precision
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE regr_slope_t1(a int, b int); openGauss=# INSERT INTO regr_slope_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT REGR_SLOPE(b,a) FROM regr_slope_t1; regr_slope ------------------ 1.78571428571429 (1 row) openGauss=# DROP TABLE regr_slope_t1;
- regr_sxx(Y, X)
描述:sum(Y^2) - sum(X)^2/N (自变量的“平方和”)
返回类型:double precision
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE regr_sxx_t1(a int, b int); openGauss=# INSERT INTO regr_sxx_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT REGR_SXX(b,a) FROM regr_sxx_t1; regr_sxx ---------- 2.8 (1 row) openGauss=# DROP TABLE regr_sxx_t1;
- regr_sxy(Y, X)
描述:sum(X*Y) - sum(X) * sum(Y)/N (自变量和因变量的“乘方积”)
返回类型:double precision
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE regr_sxy_t1(a int, b int); openGauss=# INSERT INTO regr_sxy_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT REGR_SXY(b,a) FROM regr_sxy_t1; regr_sxy ---------- 5 (1 row) openGauss=# DROP TABLE regr_sxy_t1;
- regr_syy(Y, X)
描述:sum(Y^2) - sum(X)^2/N(因变量的"平方和")
返回类型:double precision
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE regr_syy_t1(a int, b int); openGauss=# INSERT INTO regr_syy_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT REGR_SYY(b,a) FROM regr_syy_t1; regr_syy ---------- 10 (1 row) openGauss=# DROP TABLE regr_syy_t1;
- stddev(expression)
返回类型:对于浮点类型的输入返回double precision,其他输入返回numeric。
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE stddev_t1(a int, b int); openGauss=# INSERT INTO stddev_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT STDDEV(a) FROM stddev_t1; stddev ----------------------- .83666002653407554798 (1 row) openGauss=# DROP TABLE stddev_t1;
- variance(expexpression,ression)
返回类型:对于浮点类型的输入返回double precision类型,其他输入返回numeric类型。
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE variance_t1(a int, b int); openGauss=# INSERT INTO variance_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT VARIANCE(a) FROM variance_t1; variance ----------------------- .70000000000000000000 (1 row) openGauss=# DROP TABLE variance_t1;
- delta
参数:numeric
返回值类型:numeric
- checksum(expression)
描述:返回所有输入值的CHECKSUM值。使用该函数可以用来验证GaussDB数据库(不支持GaussDB之外的其他数据库)的备份恢复或者数据迁移操作前后表中的数据是否相同。在备份恢复或者数据迁移操作前后都需要用户通过手工执行SQL命令的方式获取执行结果,通过对比获取的执行结果判断操作前后表中的数据是否相同。
- 对于大表,CHECKSUM函数可能会需要很长时间。
- 如果某两表的CHECKSUM值不同,则表明两表的内容是不同的。由于CHECKSUM函数中使用散列函数不能保证无冲突,因此两个不同内容的表可能会得到相同的CHECKSUM值,存在这种情况的可能性较小。对于列进行的CHECKSUM也存在相同的情况。
- 对于时间类型timestamp, timestamptz和smalldatetime,计算CHECKSUM值时请确保时区设置一致。
- 若计算某列的CHECKSUM值,且该列类型可以默认转为TEXT类型,则expression为列名。
- 若计算某列的CHECKSUM值,且该列类型不能默认转为TEXT类型,则expression为列名::TEXT。
- 若计算所有列的CHECKSUM值,则expression为表名::TEXT。
可以默认转换为TEXT类型的类型包括:char, name, int8, int2, int1, int4, raw, pg_node_tree, float4, float8, bpchar, varchar, nvarchar, nvarchar2, date, timestamp, timestamptz, numeric, smalldatetime,其他类型需要强制转换为TEXT。
返回类型:numeric。
示例:
表中可以默认转为TEXT类型的某列的CHECKSUM值。
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE checksum_t1(a int, b int); openGauss=# INSERT INTO checksum_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT CHECKSUM(a) FROM checksum_t1; checksum ------------- 18126842830 (1 row) openGauss=# DROP TABLE checksum_t1;
表中不能默认转为TEXT类型的某列的CHECKSUM值。注意此时CHECKSUM参数是列名::TEXT。
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE checksum_t1(a int, b int); openGauss=# INSERT INTO checksum_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT CHECKSUM(a::TEXT) FROM checksum_t1; checksum ------------- 18126842830 (1 row) openGauss=# DROP TABLE checksum_t1;
表中所有列的CHECKSUM值。注意此时CHECKSUM参数是表名::TEXT,且表名前不加Schema。
1 2 3 4 5 6 7 8 9 10 11
openGauss=# CREATE TABLE checksum_t1(a int, b int); openGauss=# INSERT INTO checksum_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); openGauss=# SELECT CHECKSUM(checksum_t1::TEXT) FROM checksum_t1; checksum ------------- 11160522226 (1 row) openGauss=# DROP TABLE checksum_t1;
- mode() WITHIN GROUP (ORDER BY value anyelement)
描述:返回某列中出现频率最高的值,如果多个值频率相同,则返回最小的那个值。排序方式和该列类型的默认排序方式相同。其中value为输入参数,可以为任意类型。
返回类型:与输入参数类型相同。
示例:
openGauss=# SELECT mode() WITHIN GROUP (ORDER BY value) FROM (values(1, 'a'), (2, 'b'), (2, 'c')) v(value, tag); mode ------ 2 (1 row) openGauss=# SELECT mode() WITHIN GROUP (ORDER BY tag) FROM (values(1, 'a'), (2, 'b'), (2, 'c')) v(value, tag); mode ------ a (1 row)