Solved

need help on sql query

Posted on 2014-03-05
4
388 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.

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

773 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