How to concatenate strings of a string field in a PostgreSQL 'group by' query?

前端 未结 14 1608
北荒
北荒 2020-11-22 02:37

I am looking for a way to concatenate the strings of a field within a group by query. So for example, I have a table:

ID   COMPANY_ID   EMPLOYEE
1    1               


        
14条回答
  •  甜味超标
    2020-11-22 02:47

    You can also use format function. Which can also implicitly take care of type conversion of text, int, etc by itself.

    create or replace function concat_return_row_count(tbl_name text, column_name text, value int)
    returns integer as $row_count$
    declare
    total integer;
    begin
        EXECUTE format('select count(*) from %s WHERE %s = %s', tbl_name, column_name, value) INTO total;
        return total;
    end;
    $row_count$ language plpgsql;
    
    
    postgres=# select concat_return_row_count('tbl_name','column_name',2); --2 is the value
    

提交回复
热议问题