I have a table:
CREATE TABLE tblproducts
(
productid integer,
product character varying(20)
)
With the rows:
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');
If I execute string_agg() on tblproducts:
SELECT string_agg(product, ' | ') FROM "tblproducts"
It will return the following result:
CANDID POWDER 50 GM | ESOZ D 20 MG CAP | HHDERM CREAM 10 GM | CREAM 15 GM | KZ LOTION 50 ML | BUDECORT 200 Rotocap
How can I sort the aggregated string, in the order I would get using ORDER BY product?
I'm using PostgreSQL 9.2.4.
STRING_AGG()withORDER BY. SQL Server pages: How to get strings comma separated in ascending order with SQL Server's STRING_AGG() and How to ORDER BY with SQL Server's STRING_AGG() and SQL Server 2019 - Ordering in sub select and How to naturally ORDER BY with SQL Server's STRING_AGG()