博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PostgreSql之在group by查询下拼接列字符串
阅读量:4677 次
发布时间:2019-06-09

本文共 662 字,大约阅读时间需要 2 分钟。

首先创建group_concat聚集函数:

CREATE AGGREGATE group_concat(anyelement)(sfunc = array_append, -- 每行的操作函数,将本行append到数组里stype = anyarray, -- 聚集后返回数组类型initcond = '{}' -- 初始化空数组);

接着上一个SQL样例:

在订单明细表按poseason分组,把ticket_codeorder_id去除重复并且拼接起来

--wp_order_detailSELECT    po,    season,    array_to_string( group_concat ( DISTINCT ticket_code ), ',' ) AS ticket_codes,    array_to_string( group_concat ( DISTINCT order_id ), ',' ) AS order_idsFROM    wp_order_detailWHERE    exists (select 1 from wp_order orders where orders.id = order_id and type = 'Po')GROUP BY    po,    season;

最后查询结果截图:

402416-20180530115431258-105815976.png

转载于:https://www.cnblogs.com/VAllen/p/PostgreSql-join-string-in-group-by-select.html

你可能感兴趣的文章
Linux高级变量
查看>>
php ffmpeg
查看>>
java中== 和 .equals()的区别
查看>>
网络流学习笔记
查看>>
jquery validate
查看>>
模板函数与模板类
查看>>
WPF月视图控件
查看>>
Android指纹识别
查看>>
C#设计模式之十六观察者模式(Observer Pattern)【行为型】
查看>>
VS中的预先生成事件和后期生成事件
查看>>
JavaScript知识(二)
查看>>
Windows phone 8 学习笔记(7) 设备
查看>>
SQL Server的备份
查看>>
SQL Server 重置Identity标识列的值(INT爆了)
查看>>
07_Python的控制判断循环语句1(if判断for循环)_Python编程之路
查看>>
15_Python模块化编程_Python编程之路
查看>>
【leetcode 简单】第十七题 x 的平方根
查看>>
cocos2d-x 3.1 编译脚本android-build.py
查看>>
HDU 6319(单调队列)
查看>>
Android 常用数据操作封装类案例
查看>>