Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
SELECT string_agg( distinct a || '-' || b , ',' ORDER BY a,b) FROM table;

The above sql giving error

SQL
ERROR: in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list


How to resolve the issue.

What I have tried:

I have tried with above sql which mentioned in above problem window.
Posted
Updated 10-Nov-16 20:02pm

1 solution

With this kind of error, I typically use a subquery to do my dirty work. After playing around with this for a few minutes on SQL Fiddle[^][^], I came up with the SQL script below.

SQL
CREATE TABLE item
(
itemid integer,
item character varying(50)
);

INSERT INTO item(itemid, item)VALUES (1, 'pizza');
INSERT INTO item(itemid, item)VALUES (2, 'cake');
INSERT INTO item(itemid, item)VALUES (3, 'sandwich');


select string_agg(itemid || '-' || item,' | ') FROM ((SELECT DISTINCT item, itemid FROM item ORDER BY itemid)) t;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900