Solved

need help on sql query

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
Help on model clause 5 47
Oracle encryption 12 59
Huge BLOB data type in Oracle how to retrieve in vb.net and crystal reports 6 46
Oracle programming for starter 14 27
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

735 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