?
Solved

sql in oracle

Posted on 2013-11-06
3
Medium Priority
?
409 Views
Last Modified: 2013-11-06
Hello Experts,

I have few tables as like below:

CREATE TABLE test_aud 
(AUDITOR_ASSIGNMENTS       VARCHAR2(1000 CHAR) ) ; 


CREATE TABLE 
TEST_CATEGORY
(CATEGORY_ID           NUMBER             
,CATEGORY_NAME         VARCHAR2(255 CHAR) );


CREATE TABLE TEST_SECTION
(SECTION_NAME          VARCHAR2(255 CHAR) 
,SECTION_ID            NUMBER       );




Insert into test_aud (AUDITOR_ASSIGNMENTS) values ('F-0');
Insert into test_aud (AUDITOR_ASSIGNMENTS) values ('S-1');
Insert into test_aud (AUDITOR_ASSIGNMENTS) values ('S-3');
Insert into test_aud (AUDITOR_ASSIGNMENTS) values ('C-1040');
Insert into test_aud (AUDITOR_ASSIGNMENTS) values ('C-1000');




INSERT INTO TEST_CATEGORY (CATEGORY_ID,CATEGORY_NAME) VALUES (1040,'KP cat 102');
INSERT INTO TEST_CATEGORY (CATEGORY_ID,CATEGORY_NAME) VALUES (1000,'Antidiscrimination');


INSERT INTO TEST_SECTION (SECTION_ID,SECTION_NAME) VALUES (1,'Labor & Human Rights');
INSERT INTO TEST_SECTION (SECTION_ID,SECTION_NAME) VALUES (3,'Environment');

Open in new window



Now I have to diaplay the data from "test_aud" table  but with certain condition:

SQL> select * from test_aud;
 
AUDITOR_ASSIGNMENTS
--------------------------------------------------------------------------------
F-0
S-1
S-3
C-1040
C-1000

Open in new window


If the record is "F-0" THEN display "Facility"
If the record starts with 'S-%' then go to "TEST_SECTION" table and get the section name.
For example if it is 'S-1' then the section id will be "1" .
Similarly if the record starts with 'C-%' then go to "TEST_CATEGORY" table and get the category name table.
0
Comment
Question by:Swadhin Ray
[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
3 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 39629236
the query below should meet the need:
**Query 1**:

    select
              a.auditor_assignments
            , coalesce(c.category_name,s.section_name,'Facility') as label
    from test_aud a
    left join test_category c
           on substr(a.auditor_assignments,3,255) =  c.category_id
    left join test_section s
           on substr(a.auditor_assignments,3,255) =  s.section_id
    

**[Results][2]**:
    
    | AUDITOR_ASSIGNMENTS |                LABEL |
    |---------------------|----------------------|
    |                 F-0 |             Facility |
    |                 S-1 | Labor & Human Rights |
    |                 S-3 |          Environment |
    |              C-1040 |           KP cat 102 |
    |              C-1000 |   Antidiscrimination |



  [1]: http://sqlfiddle.com/#!4/e0a53/3

Open in new window

0
 
LVL 16

Author Closing Comment

by:Swadhin Ray
ID: 39629295
thanks
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39629308
no problem; thank you! cheers, Paul
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
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…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Suggested Courses

741 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