PostgreSQL数据库中group_concat函数的定义

group_concat函数的自定义

CREATE OR REPLACE FUNCTION group_concat_agg(text, anyelement,text)
RETURNS text AS $$
BEGIN
  RETURN CASE
    WHEN $2 IS NULL THEN $1
    WHEN $1 IS NULL THEN $2::text
    ELSE $1 || $3 || $2::text
  END;
END;
$$ IMMUTABLE LANGUAGE plpgsql;

CREATE AGGREGATE group_concat(anyelement,text) (
  SFUNC = group_concat_agg,
  STYPE = text
);


CREATE OR REPLACE FUNCTION group_concat_agg(text, anyelement)
RETURNS text AS $$
BEGIN
  RETURN CASE
    WHEN $2 IS NULL THEN $1
    WHEN $1 IS NULL THEN $2::text
    ELSE $1 || ',' || $2::text
  END;
END;
$$ IMMUTABLE LANGUAGE plpgsql;

CREATE AGGREGATE group_concat(anyelement) (
  SFUNC = group_concat_agg,
  STYPE = text
);

-- CREATE OR REPLACE FUNCTION group_concat_agg(text, anyelement,text,anyelement)
-- RETURNS text AS $$
-- BEGIN
--   RETURN CASE
--     WHEN ($2 IS NULL) or ($4 is null) THEN $1
--     WHEN $1 IS NULL THEN $2::text || $3 || $4::text
--     ELSE $1 || ',' || $2::text || $3 || $4::text
--   END;
-- END;
-- $$ IMMUTABLE LANGUAGE plpgsql;

-- CREATE AGGREGATE group_concat(anyelement,text,anyelement) (
--   SFUNC = group_concat_agg,
--   STYPE = text
-- );

CREATE OR REPLACE FUNCTION group_concat_agg(text, text,text,bigint)
RETURNS text AS $$
BEGIN
  RETURN CASE
    WHEN ($2 IS NULL) or ($4 is null) THEN $1
    WHEN $1 IS NULL THEN $2::text || $3 || $4::text
    ELSE $1 || ',' || $2::text || $3 || $4::text
  END;
END;
$$ IMMUTABLE LANGUAGE plpgsql;

CREATE AGGREGATE group_concat(text,text,bigint) (
  SFUNC = group_concat_agg,
  STYPE = text
);

测试

select "id","type" from t_rule;
select * from t_resource;

select "type",group_concat("id",',') from t_rule group by "type";
select group_concat("id",',') from t_rule where "type" = 'SWITCH';
select group_concat("id") from t_rule where "type" = 'SQL';
select group_concat("id") from t_rule;

select "type",group_concat("id",',') from t_resource group by "type";

select "type",group_concat(concat(TO_CHAR(created_at,'YYYY-MM-DD'),'_',"id"),',') from t_resource group by "type";
select "type",group_concat(TO_CHAR(created_at,'YYYY-MM-DD'),'_',"id") from t_resource group by "type";

select concat(TO_CHAR(created_at,'YYYY-MM-DD'),'_',"id") from t_resource;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值