openGauss SQL参考—函数和操作符:聚集函数(3)

  • 聚集列是时间类型。

    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;
posted @ 2024-07-31 09:36  openGauss-bot  阅读(98)  评论(0)    收藏  举报