Solved

need help on sql query

Posted on 2014-03-05
4
385 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
  • 2
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now