如何对string_agg()的结果进行排序

Vivek S .:

我有一张桌子:

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

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

我正在使用PostgreSQL 9.2.4。

伊戈尔·罗曼琴科(Igor Romanchenko):

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

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

详细信息在这里

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章