Conditional Query in Oracle

Posted on 2014-08-16
Last Modified: 2014-08-22

I need suggestion on a conditional query in Oracle like the followings.
say I have a table TBL1 with field1,field2 and field3.
I want to have select statement like the following

if TBL1.field1= 'X' then TBL1 left join TBL2
if TBL1.field1= 'Y' then TBL1 left join TBL3
if TBL1.field1= 'Z' then TBL1 left join TBL4

Question by:mrong
    LVL 32

    Accepted Solution

    SELECT	T1.*,
    FROM	TBL1 T1
    	LEFT JOIN TBL2 T2 ON <JoinCondition> AND T1.field1 = 'X'
    	LEFT JOIN TBL3 T3 ON <JoinCondition> AND T1.field1 = 'Y'
    	LEFT JOIN TBL4 T4 ON <JoinCondition> AND T1.field1 = 'Z';

    Open in new window

    LVL 31

    Expert Comment

    Is field1 in tbl1 always going to contain only 'X' or "Y" or 'Z"?

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
    This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
    This video shows how to recover a database from a user managed backup

    729 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

    20 Experts available now in Live!

    Get 1:1 Help Now