?
Solved

need help on sql query

Posted on 2014-03-05
4
Medium Priority
?
395 Views
Last Modified: 2014-03-05
Hi ,

I have an table called customer with the below structure.

customer_id
is_internal
is_external
is_vip


The fields is_internal, is_external,is_vip store values like Y or N.

My requirement is I need to check these fields for each customer Id , and sort them by concantenating values present in is_internal, is_external,is_vip.


for example
customer id         Is_internal is_external is_vip                   customer type
1                                Y                 Y               Y                           internal,external,vip
2                                Y                 Y              N                           internal,external
3                                Y                N              Y                            internal, vip
4                                N               Y               Y                            external, vip
5                               N               Y                N                           external
6                               N              N               Y                              vip

i must write an query which fetches the results as in customer_id , customer_type
and sort based on customer_type.

Select customer_id ,
case when is_internal ='Y' and is_external='Y' and is_vip='Y' then
      'INTERNAL'||','||'EXTERNAL'||','||'VIP'
when is_internal ='Y' and is_external='Y' and is_vip='N' then
      'INTERNAL'||','||'EXTERNAL'
when is_internal ='Y' and is_external='N' and is_vip='N' then
      'INTERNAL'
WHEN is_internal ='Y' and is_external='N' and is_vip='Y' then
      'INTERNAL'||','||'VIP'
When is_internal ='N' and is_external='N' and is_vip='Y' then
      'VIP'
when is_internal ='N' and is_external='Y' and is_vip='Y' then  
'EXTERNAL'||'VIP'
when is_internal ='N' and is_external='Y' and is_vip='N' then
'EXTERNAL'
end customertype
from customer
order by 2 asc

Is there an better approach to this requirement.  can I use list_agg function to achive the result.

Any help is really appreciated.
0
Comment
Question by:sam_2012
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 2000 total points
ID: 39906932
Use DECODE() function:
. . . DECODE(is_internal, 'Y','INTERNAL,','')||DECODE(is_external,'Y','EXTERNAL,','')||DECODE(is_vip,'Y','VIP')

Open in new window

0
 

Author Comment

by:sam_2012
ID: 39907004
when only is_internal is true , Iam getting INTERNAL, . I do not want the , to be appended , if only one condition is satisfied

For example :
is_internal="Y' and is_external='N' and is_vip='N'

it should display only INTERNAL and not INTERNAL,
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39907038
And is it too difficult for you to remove the trailing comma?

HINT: Use the RTRIM() function.
0
 

Author Closing Comment

by:sam_2012
ID: 39908249
thanks a lot.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question