Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

listagg data to useable format?

This is my first time working with the LISTAGG function and I'm confused. I can select the data easily enough, but the characters of the USERS column all have spaces in between them, and when trying to copypaste it, no data from that column is copied. I've tried with two different IDEs. Am I doing something wrong?

Example:

select course_id, listagg(firstname, ', ') within group (order by course_id) as users
    from (
      select distinct u.firstname, u.lastname, u.student_id, cm.course_id
      from course_users cu
      join users u on u.pk1 = cu.users_pk1
      join course_main cm on cm.pk1 = cu.crsmain_pk1
      and cm.course_id like '2015SP%'
      )
group by course_id;

Yields:

LISTAGG result

like image 483
Cliff Avatar asked Mar 02 '15 14:03

Cliff


People also ask

What does Listagg return?

Returns the concatenated input values, separated by the delimiter string.

What is alternative for Listagg in Oracle?

Alternative to LISTAGG Function is to create a user Function our self and here is what the Function looks like. Using the Function (ge_employee_names) in Select to generate LISTAGG result.

Can we use Listagg in Oracle Forms?

You cannot use LISTAGG function directly in forms. Instead you can create a record group, which uses the LSTAGG and use the record group. Or you can create DB procedure/Function and call that in forms.

Can we use Listagg without GROUP BY?

Usage of the LISTAGG Function There are a few ways you can use this function. If you use it without any grouping, LISTAGG operates on all rows and returns a single row. If you use it with grouping, LISTAGG operates on and returns a row for each group defined by the GROUP BY clause.


2 Answers

I had similar problem, it turned out that the problem was with encoding. I got this solved like this (change to another encoding if needed):

...listagg(convert(firstname, 'UTF8', 'AL16UTF16'), ', ')...
like image 167
ttaaoossuuuu Avatar answered Sep 26 '22 02:09

ttaaoossuuuu


Your firstname column seems to be defined as nvarchar2:

with t as (
  select '2015SP.BOS.PPB.556.A'as course_id,
    cast('Alissa' as nvarchar2(10)) as firstname
  from dual
  union all select '2015SP.BOS.PPB.556.A'as course_id,
    cast('Dorothea' as nvarchar2(10)) as firstname
  from dual
)
select course_id, listagg(firstname, ', ')
  within group (order by course_id) as users
from t
group by course_id;

COURSE_ID            USERS                         
-------------------- ------------------------------
2015SP.BOS.PPB.556.A 

... and I can't copy/paste the users values from SQL Developer either, but it displays with spaces, as you can see from SQL*Plus:

COURSE_ID            USERS
-------------------- ------------------------------
2015SP.BOS.PPB.556.A  A l i s s a,  D o r o t h e a

As the documentation says, the listagg() function always returns varchar2 (or raw), so passing in an nvarchar2 value causes an implicit conversion which is throwing out your results.

If you're stuck with your column being of that data type, you could cast it to varchar2 inside the listagg call:

column users format a30
with t as (
  select '2015SP.BOS.PPB.556.A'as course_id,
    cast('Alissa' as nvarchar2(10)) as firstname
  from dual
  union all select '2015SP.BOS.PPB.556.A'as course_id,
    cast('Dorothea' as nvarchar2(10)) as firstname
  from dual
)
select course_id, listagg(cast(firstname as varchar2(10)), ', ')
  within group (order by course_id) as users
from t
group by course_id;

COURSE_ID            USERS                         
-------------------- ------------------------------
2015SP.BOS.PPB.556.A Alissa, Dorothea               

But you probably don't really want it to be nvarchar2 at all.

like image 29
Alex Poole Avatar answered Sep 26 '22 02:09

Alex Poole