如何对string_agg()的结果进行排序 [英] How to sort the result from string_agg()

查看:3270
本文介绍了如何对string_agg()的结果进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子:

CREATE TABLE tblproducts
(
productid integer,
product character varying(20)
)

带有行:

INSERT INTO tblproducts(productid, product) VALUES (1, 'CANDID POWDER 50 GM');
INSERT INTO tblproducts(productid, product) VALUES (2, 'SINAREST P SYP 100 ML');
INSERT INTO tblproducts(productid, product) VALUES (3, 'ESOZ D 20 MG CAP');
INSERT INTO tblproducts(productid, product) VALUES (4, 'HHDERM CREAM 10 GM');
INSERT INTO tblproducts(productid, product) VALUES (5, 'CREAM 15 GM');
INSERT INTO tblproducts(productid, product) VALUES (6, 'KZ LOTION 50 ML');
INSERT INTO tblproducts(productid, product) VALUES (7, 'BUDECORT 200 Rotocap');

如果我在 string_agg()上执行 tblproducts

SELECT string_agg(product, ' | ') FROM "tblproducts"

它将返回以下结果:

CANDID POWDER 50 GM | ESOZ D 20 MG CAP | HHDERM CREAM 10 GM | CREAM 15 GM | KZ LOTION 50 ML | BUDECORT 200 Rotocap

如何按<$ c使用的顺序对聚合字符串进行排序$ c> ORDER BY product ?

我正在使用PostgreSQL 9.2.4。

I'm using PostgreSQL 9.2.4.

推荐答案

使用postgres 9.0+,您可以编写:

With postgres 9.0+ you can write:

select string_agg(product,' | ' order by product) from "tblproducts"

此处的详细信息

这篇关于如何对string_agg()的结果进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆