views:

317

answers:

1

I am trying to get the string_agg function, detailed here to work with the distinct keyword. Sample usage:

select string_agg(distinct a.name), a.id from tbl_a a group by a.id

The problem is that this works in sqldeveloper, but when running it in application express in an anonymous pl/sql block, it refuses to accept the distinct keyword and I get an error saying that the distinct is not allowed. Why would it be doing this? Is there a simple workaround? I am using 10.2 and application express 3.2

Edit: to clarify, I am interested in solving this problem, but if there is another string_agg/group_concat function that I can use I am open to any ideas.

Edit: I eventually want to be able to do something like

select string_agg(distinct a.name),string_agg(distinct a.city), a.id from tbl_a a group by a.id

and have both string_agg columns be deduped... this is a problem when using a distinct in a subquery as it does not get the distinct values for both columns...

+1  A: 

This behaviour seems to be a bug according to the last post in this AskTom thread.

Moving the distinct operation into a sub query should work:

select   string_agg(sq.name)
        ,sq.id 
from     (
         select distinct 
                a.name
               ,a.id 
         from   tbl_a a 
         ) sq
group by sq.id
Juergen Hartelt
this would work for string_agg on a single column... but if I want to do multiple columns I have to do multiple subqueries.
jle
true. Another advice in that thread is to use dynamic sql. That approach would allow you to use the exact same statement, which you can use outside a pl/sql block. You could take a look at http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28370/dynamic.htm#i13130 to get started.
Juergen Hartelt