聚集函数

聚集函数

  • sum(expression)

    描述:所有输入行的expression总和。

    返回类型:

    通常情况下输入数据类型和输出数据类型是相同的,但以下情况会发生类型转换:

    • 对于SMALLINT或INT输入,输出类型为BIGINT。
    • 对于BIGINT输入,输出类型为NUMBER 。
    • 对于浮点数输入,输出类型为DOUBLE PRECISION。

    示例:

    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) 
    openGauss=# DROP TABLE tab;
    
  • max(expression)

    描述:所有输入行中expression的最大值。

    参数类型:任意数组、数值、字符串、日期/时间类型、IPV4和IPV6地址(INET型和CIDR型)。

    返回类型:与参数数据类型相同

    示例:

    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)

    描述:所有输入行中expression的最小值。

    参数类型:任意数组、数值、字符串、日期/时间类型、IPV4和IPV6地址(INET型和CIDR型)。

    返回类型:与参数数据类型相同

    示例:

    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类型。

    否则和输入数据类型相同。

    示例:

    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)

    描述:返回表中满足expression不为NULL的行数。

    返回类型:BIGINT

    示例:

    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

    示例:

    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类型

    示例:

    openGauss=# SELECT median(id) FROM (values(1), (2), (3), (4), (null)) test(id); 
     median 
    -------- 
         2.5 
    (1 row)
    
  • array_agg(expression)

    描述:将所有输入值(包括空)连接成一个数组。

    返回类型:参数类型的数组。

    示例:

    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)

    描述:将输入值连接成为一个字符串,用分隔符分开。

    返回类型:和参数数据类型相同。

    示例:

    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

    示例:

    聚集列是文本字符集类型。

    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;
    

    聚集列是整型。

    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;
    

    聚集列是浮点类型。

    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;
    

    聚集列是时间类型。

    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;
    

    聚集列是时间间隔类型。

    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;
    

    分隔符缺省时,默认为空。

    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列为对应分组的有序聚集。

    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;
    
  • group_concat([DISTINCT | ALL] expression [,expression …] [ORDER BY { expression [ [ ASC | DESC | USING operator ] | nlssort_expression_clause ] [ NULLS { FIRST | LAST } ] } [,…]] [SEPARATOR str_val])

    描述:(仅在B模式下可用)参数数量不定,可对多列进行拼接,将聚集列数据按照ORDER BY指定的排序方式排列,并用separator指定的分隔符拼接成一个字符串, 不支持作为窗口函数使用。

    • DISTINCT:可选,表示对每行拼接后结果进行去重。
    • expression: 必选,指定聚集列名或基于列的有效表达式。
    • ORDER BY: 可选,后跟可变数量表达式及排序规则。group_concat函数中不支持(order by + 数字)这种形式。
    • SEPARATOR子句: 可选,后跟CONST字符(串),分组中相邻两行表达式结果使用此分隔符拼接。若不指定,默认使用英文逗号‘,’。
    • 当同时指定DISTINCT和ORDER BY时,openGauss的所有order by表达式必须在distinct表达式中,否则报错。

    返回类型:text

    示例:

    使用separator指定分隔符为';'。

    openGauss=# CREATE TABLE group_concat_t1(a int, b int); 
    
    openGauss=# INSERT INTO group_concat_t1 VALUES (NULL,1),(1,2),(1,3),(2,4),(2,5),(3,6); 
    
    openGauss=# SELECT a,group_concat(b separator ';') FROM group_concat_t1 GROUP BY a ORDER BY a; 
     a | group_concat  
    ---+-------------- 
     1 | 2;3 
     2 | 4;5 
     3 | 6 
       | 1 
    (4 rows) 
    
    openGauss=# DROP TABLE group_concat_t1;
    

    分隔符缺省时,默认为','。

    openGauss=# CREATE TABLE group_concat_t1(a int, b int); 
    
    openGauss=# INSERT INTO group_concat_t1 VALUES (NULL,1),(1,2),(1,3),(2,4),(2,5),(3,6); 
    
    openGauss=# SELECT a,group_concat(a,b) FROM group_concat_t1 GROUP BY a ORDER BY a; 
     a | group_concat  
    ---+-------------- 
     1 | 12,13 
     2 | 24,25 
     3 | 36 
       | 1 
    (4 rows) 
    
    openGauss=# DROP TABLE group_concat_t1;
    

    聚集列是文本字符集类型。

    openGauss=# CREATE TABLE group_concat_t1(a int, b text); 
    
    openGauss=# INSERT INTO group_concat_t1 VALUES (NULL,'a1'),(1,'b2'),(1,'c3'),(2,'d4'),(2,'e5'),(3,'f6'); 
    
    openGauss=# SELECT a,group_concat(a,b) FROM group_concat_t1 GROUP BY a ORDER BY a; 
     a | group_concat  
    ---+-------------- 
     1 | 1b2,1c3 
     2 | 2d4,2e5 
     3 | 3f6 
       | a1 
    (4 rows) 
    
    openGauss=# DROP TABLE group_concat_t1;
    

    聚集列是整型。

    openGauss=# CREATE TABLE group_concat_t1(a int, b int); 
    
    openGauss=# INSERT INTO group_concat_t1 VALUES (NULL,1),(1, 2),(1, 3),(2, 4),(2, 5),(3,6); 
    
    openGauss=# SELECT a,group_concat(b) FROM group_concat_t1 GROUP BY a ORDER BY a; 
     a | group_concat  
    ---+-------------- 
     1 | 2,3 
     2 | 4,5 
     3 | 6 
       | 1 
    (4 rows) 
    
    openGauss=# DROP TABLE group_concat_t1;
    

    聚集列是浮点类型。

    openGauss=# CREATE TABLE group_concat_t1(a int, b float); 
    
    openGauss=# INSERT INTO group_concat_t1 VALUES (NULL,1.11),(1,2.22),(1,3.33),(2,4.44),(2,5.55),(3,6.66); 
    
    openGauss=# SELECT a,group_concat(b) FROM group_concat_t1 GROUP BY a ORDER BY a; 
     a | group_concat  
    ---+-------------- 
     1 | 2.22,3.33 
     2 | 4.44,5.55 
     3 | 6.66 
       | 1.11 
    (4 rows) 
    
    openGauss=# DROP TABLE group_concat_t1;
    

    聚集列是时间类型。

    openGauss=# CREATE TABLE group_concat_t1(a int, b timestamp); 
    
    openGauss=# INSERT INTO group_concat_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,group_concat(b) FROM group_concat_t1 GROUP BY a ORDER BY a; 
     a |              group_concat                
    ---+----------------------------------------- 
     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 group_concat_t1;
    

    聚集列是二进制类型。

     openGauss=# CREATE TABLE group_concat_t1(a int, b bytea); 
    
    openGauss=# INSERT INTO group_concat_t1 VALUES (NULL,'1'),(1,'2'),(1,'3'),(2,'4'),(2,'5'),(3,'6'); 
    
    openGauss=# SELECT a,group_concat(b) FROM group_concat_t1 GROUP BY a ORDER BY a; 
     a | group_concat  
    ---+-------------- 
     1 | \x32,\x33 
     2 | \x34,\x35 
     3 | \x36 
       | \x31 
    (4 rows) 
    
    openGauss=# DROP TABLE group_concat_t1;
    

    聚集列是时间间隔类型。

    openGauss=# CREATE TABLE group_concat_t1(a int, b interval); 
    
    openGauss=# INSERT INTO group_concat_t1 VALUES (NULL,'1 days'),(1,'2 days'),(1,'3 days'),(2,'4 days'),(2,'5 days'),(3,'6 days'); 
    
    openGauss=# SELECT a,group_concat(b) FROM group_concat_t1 GROUP BY a ORDER BY a; 
     a | group_concat   
    ---+--------------- 
     1 | 2 days,3 days 
     2 | 4 days,5 days 
     3 | 6 days 
       | 1 day 
    (4 rows) 
    
    openGauss=# DROP TABLE group_concat_t1;
    

    使用distinct去重。

    openGauss=# CREATE TABLE group_concat_t1(a int, b interval); 
    
    openGauss=# INSERT INTO group_concat_t1 VALUES (NULL,'1 days'),(1,'2 days'),(1,'2 days'),(1,'3 days'),(1,'3 days'),(2,'4 days'),(2,'5 days'),(3,'6 days'); 
    
    openGauss=# SELECT a,group_concat(distinct b) FROM group_concat_t1 GROUP BY a ORDER BY a; 
     a | group_concat   
    ---+--------------- 
     1 | 2 days,3 days 
     2 | 4 days,5 days 
     3 | 6 days 
       | 1 day 
    (4 rows) 
    
    openGauss=# DROP TABLE group_concat_t1;
    

    使用order by排序。

    openGauss=# CREATE TABLE group_concat_t1(a int, b interval); 
    
    openGauss=# INSERT INTO group_concat_t1 VALUES (NULL,'1 days'),(1,'2 days'),(1,'3 days'),(2,'4 days'),(2,'5 days'),(3,'6 days'); 
    
    openGauss=# SELECT a,group_concat(b ORDER BY b desc) FROM group_concat_t1 GROUP BY a ORDER BY a; 
     a | group_concat   
    ---+--------------- 
     1 | 3 days,2 days 
     2 | 5 days,4 days 
     3 | 6 days 
       | 1 day 
    (4 rows) 
    
    openGauss=# DROP TABLE group_concat_t1;
    
  • covar_pop(Y, X)

    描述:总体协方差。

    返回类型:double precision

    示例:

    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

    示例:

    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。

    示例:

    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。

    示例:

    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类型。

    示例:

    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类型。

    示例:

    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 。

    返回类型:和参数数据类型相同。

    示例:

    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。

    返回类型:和参数数据类型相同

    示例:

    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

    示例:

    openGauss=# SELECT bool_and(100 <2500);
     bool_and
    ----------
     t
    (1 row)
    
  • bool_or(expression)

    描述:如果所有输入值只要有一个为真,则为真,否则为假。

    返回类型:bool

    示例:

    openGauss=# SELECT bool_or(100 <2500);
     bool_or
    ----------
     t
    (1 row)
    
  • corr(Y, X)

    描述:相关系数

    返回类型:double precision

    示例:

    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_and。

    返回类型:bool

    示例:

    openGauss=# SELECT every(100 <2500);
     every
    -------
     t
    (1 row)
    
  • regr_avgx(Y, X)

    描述:自变量的平均值 (sum(X)/N)

    返回类型:double precision

    示例:

    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)

    描述:因变量的平均值 (sum(Y)/N)

    返回类型:double precision

    示例:

    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)

    描述:两个表达式都不为NULL的输入行数。

    返回类型:bigint

    示例:

    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

    示例:

    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

    示例:

    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

    示例:

    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(X^2) - sum(X)^2/N (自变量的“平方和”)

    返回类型:double precision

    示例:

    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

    示例:

    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(Y)^2/N(因变量的"平方和")

    返回类型:double precision

    示例:

    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)

    描述:stddev_samp的别名。

    返回类型:对于浮点类型的输入返回double precision,其他输入返回numeric。

    示例:

    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)

    描述:var_samp的别名。

    返回类型:对于浮点类型的输入返回double precision类型,其他输入返回numeric类型。

    示例:

    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值。使用该函数可以用来验证openGauss数据库(不支持openGauss之外的其他数据库)的备份恢复或者数据迁移操作前后表中的数据是否相同。在备份恢复或者数据迁移操作前后都需要用户通过手工执行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值。

      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。

      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。

      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;
    
  • first(anyelement)

    描述:返回第一个非NULL输入。

    返回类型:anyelement

    openGauss=# select * from tba;
    name 
    -----
    A    
    A    
    D    
    (4 rows)
    
    openGauss=# select first(name) from tba;
    first
    -----
    A
    (1 rows)
    
  • last(anyelement)

    描述:返回最后一个非NULL输入。

    返回类型:anyelement

    openGauss=# select * from tba;
    name 
    -----
    A    
    A    
    D    
    (4 rows)
    
    openGauss=# select last(name) from tba;
    last
    -----
    D
    (1 rows)
    
  • 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)
    
  • json_agg(any)

    描述:将值聚集为json数组。

    返回类型:array-json

    示例:

    openGauss=# CREATE TABLE json_agg_t1(a int, b int); 
    
    openGauss=# INSERT INTO json_agg_t1 VALUES (NULL,11),(1,2),(1,3),(2,4),(2,5),(3,6); 
    
    openGauss=# SELECT JSON_AGG(a) FROM json_agg_t1; 
           json_agg         
    ----------------------- 
     [null, 1, 1, 2, 2, 3] 
    (1 row) 
    
    openGauss=# DROP TABLE json_agg_t1;
    
  • json_object_agg(any, any)

    描述:将值聚集为json对象。

    返回类型:object-json

    示例:

     openGauss=# CREATE TABLE json_object_agg_t1(a int, b int); 
    
    openGauss=# INSERT INTO json_object_agg_t1 VALUES (11,NULL),(1,2),(1,3),(2,4),(2,5),(3,6); 
    
    openGauss=# SELECT JSON_OBJECT_AGG(a,b) FROM json_object_agg_t1; 
                           json_object_agg                         
    -------------------------------------------------------------- 
     { "11" : null, "1" : 2, "1" : 3, "2" : 4, "2" : 5, "3" : 6 } 
    (1 row) 
    
    openGauss=# DROP TABLE json_object_agg_t1;
    
意见反馈
编组 3备份
    openGauss 2024-04-27 00:42:13
    取消