Solved

sql in oracle

Posted on 2013-11-06
3
356 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
  • 2
3 Comments
 
LVL 48

Accepted Solution

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

Expert Comment

by:PortletPaul
ID: 39629308
no problem; thank you! cheers, Paul
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…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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 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…

743 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

11 Experts available now in Live!

Get 1:1 Help Now