Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How can I get nested case statements to work with listagg?

64c067aa-cb34-4b1d-ad35-2dc80ca18e3dSep 16 2015 — edited Sep 17 2015

I have a statement below which is not working the way I want it to, here is my code:

           select

                case when o.personal_label is not null

                     then

                         listagg(case when s.subcodevalue like '%MON%'

                                 then s.subcodevalue||' '||o.personal_label

                                 else s.subcodevalue

                                 end,': ')

                         within group (order by s.subcodevalue) as subcodevalue

                     else 

                         listagg(s.subcodevalue,': ')

                         within group (order by s.subcodevalue) as subcodevalue

                end

           from mtm_styles_new s, order_mtm o

           where INSTR(o.extras,s.code) > 0

                and o.bodyfitting=s.bodyfitting

                and o.division=s.division

                and s.subcodevalue is not null

                and o.ORDER_MTMID = 'somevalue'

It should bring back the following

*MON TESTING: PSUR

or the following if the first case is not met:

*MON: PSUR

I am getting an error saying "missing keyword", but can't make out where, can someone please assist me here?

The following works fine as it is not a nested case statement but I need to nest the case statement:

    select  listagg(case when s.subcodevalue like '%MON%'

                                 then s.subcodevalue||' '||o.personal_label

                                 else s.subcodevalue

                                 end,': ')

                         within group (order by s.subcodevalue) as subcodevalue

                         from mtm_styles_new s, order_mtm o

  where INSTR(o.extras,s.code) > 0

  and o.bodyfitting=s.bodyfitting

  and o.division=s.division

  and s.subcodevalue is not null

  and o.ORDER_MTMID = 'somevalue';

This post has been answered by BrendanP on Sep 16 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 15 2015
Added on Sep 16 2015
4 comments
8,383 views