Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

I want to combine 4 separate queries into one SQL statement

1003785Apr 17 2013 — edited Apr 18 2013
Hello and thank you in advance for your help.
This is my first post to this forum.

I have 4 tables that I am querying via Java individually then compare the results programmatically where the first query to return results, the remaining tables (if any) are not queried and the program continues.

I want to combine this into one SQL query and return the appropriate results without the help of performing comparisons in my program.

Each table is basically the same, but they each store different types of the data.

Here is an example of each table:

Earnings:
customerID
levelNumber
effectiveDate
amount

Bonus:
customerID
levelNumber
effectiveDate
amount

Savings:
customerID
levelNumber
effectiveDate
amount

Surcharges:
customerID
levelNumber
effectiveDate
amount


What I want to happen in a single query is to:
1) select customerId, effectiveDate from Earnings where levelNumber = 4
if no results found
2) select customerId, effectiveDate from Bonus where levelNumber = 4
if no results found
3) select customerId, effectiveDate from Savings where levelNumber = 4
if no results found
4) select customerId, effectiveDate from Surcharges where levelNumber = 4
if no results found in any of the tables, return nothing.

I do not want to write something in PL/SQL or a stored procedure.

Does this seem to far-fetched of an idea?
Would it be easier to just continue with my current method of running a query against each table separately and compare the results in my program?

Thank you again for your help.

-Tim
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 16 2013
Added on Apr 17 2013
3 comments
599 views