141

私はテーブルを持っています:

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 を使用しています。

4

3 に答える 3

3
select string_agg(prod,' | ') FROM 
  (SELECT product as prod FROM tblproducts ORDER BY product )MAIN;

SQL フィドル

于 2014-07-23T09:37:56.883 に答える