postgresql拓展if、ifnull、group_concat函数
postgresql版本是8.2.15。
最近陆续有数据分析师从impala、hive转到查询gpdb,gpdb虽然能够支持在查询语句中带多个distinct,但是缺少相应的if、ifnull、group_concat函数,正好年后有空就拓展一些函数给他们用
1. to_array聚集函数
CREATE AGGREGATE pg_catalog.to_array (anyelement)
(
sfunc = array_append,
stype = anyarray,
initcond = '{}'
);
2. if函数
create function if(expr bool, true_result anyelement, false_result anyelement) RETURNS anyelement AS
$$
BEGIN
if expr then return true_result;
else return false_result;
end if;
END;
$$
LANGUAGE plpgsql;
3. ifnull函数
create function ifnull(value anyelement, null_value anyelement) RETURNS anyelement AS
$$
BEGIN
if value is null then return null_value;
else return value;
end if;
END;
$$
LANGUAGE plpgsql;
if第2、3参数和ifnull的两个参数需要指明其中一个参数的类型,而且类型要一样,比如ifnull('a'::text, 'b'),由于两个参数都是输出,因此他们的类型必须一致,指出其中一个参数类型,另一个参数的类型也就确定了。常量参数必须要指明其中一个参数的类型,但表字段本身就有类型,因此不需要特意指出类型,如ifnull(city_name, 'b'),同样也要注意两个参数类型一致,不能写ifnull(city_name, 3)
其中用聚组函数to_array配合distinct语句、array_to_string函数来模拟group_concat函数的功能,例如group_concat(city_name, ',')可以改成array_to_string(to_array(distinct city_name), ',')
为什么不直接写一个group_concat聚组函数呢?因为pg的的聚组函数只能从一种类型转变成另一种,不能连续转变成第二种,因此普通字段需要先转变成数组,然后再变成字符串。
那么为什么不先把字段变成字符串,然后拼接在一起呢?因为postgresql8.2.15版本没有任何一个去重函数,只能用distinct语句来去重,同时distinct只能用在“只有一个参数的聚组函数”中,对于group_concat(distinct city_name, ',')这条语句,group_concat有两个参数,语法有错。这么一来二去,只能用两层函数去实现group_concat了
2017年2月9号发现postgresql原生就有一个array_agg聚合函数,相当于to_array的功能
distinct和group的区别
distinct: 适用于重复度高、值可枚举、种类少的字段,因为ditinct保存在内存中,如果种类太多内存会爆,去重速度快
group: 生成临时表,速度慢,没有内存爆的问题
postgresql拓展if、ifnull、group_concat函数的更多相关文章
- 【mysql】group_concat函数substring_index函数
1.group_concat函数:用于将多个字符串连接成一个字符串 用法规则: SELECT GROUP_CONCAT(拼接的字段) from tableName; 使用:查询basic_projec ...
- group_concat函数详解
来自: http://hchmsguo.iteye.com/blog/555543 MySQL中group_concat函数 完整的语法如下: group_concat([DISTINCT] 要连接的 ...
- group_concat函数与find_in_set()函数相结合
一:group_concat函数详解 1.语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'] ...
- 教您如何使用MySQL group_concat函数
MySQL group_concat函数是典型的字符串连接函数,下面就为您介绍MySQL group_concat的语法,希望对您学习MySQL group_concat函数有所帮助. MySQL g ...
- mysql中group_concat函数用法
该函数返回带有来自一个组的连接的非NULL值的字符串结果.该函数是一个增强的Sybase SQL Anywhere支持的基本LIST()函数. 语法结构: GROUP_CONCAT([DISTINCT ...
- Mysql group_concat函数被截断的问题
mysql group_concat函数被截断的问题 MySQL的 group_concat 函数默认返回1024个字节长度,超过长度的会被截断.最近程序中就遇到这个问题了. 通过如下命令可以查看 ...
- mysql中的group_concat函数的用法
本文通过实例介绍了MySQL中的group_concat函数的使用方法,比如select group_concat(name) . MySQL中group_concat函数 完整的语法如下: grou ...
- Mysql 列转行group_concat函数,与行转列
1.正常情况. SELECT JoinEventIds from nt_mainnum 2.使用group_concat函数 select group_concat(JoinEventIds) fro ...
- mysql group_concat函数是有长度限制的
在表关联查询中,特别是一对多关系的表查询中,group_concat函数是很有用的一个函数,帮助我们减少对数据库查询的次数,减少服务器的压力. 但是今天使用group_concat函数查询数据库时,发 ...
随机推荐
- hiho1560 - 矩阵快速幂
题目链接 坑死了,以为是K进制数,每一位可以是0-K之间的,其实是十进制,每一位最高为9,一直wa在这....... ----------------------------------------- ...
- [转]C++ 获取文件夹下的所有文件名
转自http://www.cnblogs.com/fnlingnzb-learner/p/6424563.html 头文件:#include<io.h> char * filePath = ...
- 此请求的查询字符串的长度超过配置的 maxQueryStringLength 值
异常详细信息: System.Web.HttpException: 此请求的查询字符串的长度超过配置的maxQueryStringLength 值. 我碰到此问题出现的原因是重写了HttpModule ...
- Docker中免去sudo的设置方法
Add the docker group if it doesn't already exist: sudo groupadd docker Add the connected user " ...
- WordPress开启伪静态
一.NGINX 的话在 domain.conf 的 server 增加代码: location / { try_files $uri $uri/ /index.php?$args; } 如果使用的是 ...
- iptables 配置端口及转发
iptables端口转发指令:iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 8443 iptables配 ...
- qume-kvm 命令管理
sudo /etc/init.d/acpid start 安装管理包工具 sudo apt install libguestfs-tools [ qemu-kvm qemu-kvm-tools vir ...
- PatentTips - Controlling voltage and frequency
BACKGROUND OF THE INVENTION Mobile devices, such as but not limited to personal data appliances, cel ...
- [Perl系列—] 2. Perl 中的引用使用方法
Perl 中的引用,为什么要使用引用? 对于熟悉C语言的开发人员来说, 指针这个概念一定不陌生. Perl 的引用就是指针,能够指向变量.数组.哈希表甚至子程序. Perl5中的两种Perl引用类型为 ...
- Double Vision (Unity 5.0)
Double Vision (Unity 5.0): 根据 http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter03.html ...