Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Oracle SqlPlus Conditional Union

Posted on 2006-11-08
1
Medium Priority
?
2,853 Views
Last Modified: 2008-01-09
I have two tables (Table_rea and table_estimate) with the same fields and i need to create union including contional field with the first table.

Table 1.

select month, customer, sum(bill) as bill_month
from table_real
where date_bill between '20060901' and '20060930'
group by month,customer
union
select month, customer, sum(bill) as bill_month
from table_estimate
where date_bill between '20060901' and '20060930'
group by month,customer

The table_real in september have one record and i need join the next record estimate, but when the user make a query in octubre i do not need estimate becouse this moment i know the other real record
Example1. (situation in September)
Table_real
Month,  customer, bill
09        MR0015    $20
XX        XXXXX     XXX  (this moment i do not know this record each month we need to record)

Table_estimate
Month, customer, bill
09       MR0015    $17  (i need join this record the table_real)
-----------------------------------------------------------------------------------
Example 2 (Situation in october)
 Month,  customer, bill
09        MR0015    $20
09        MR0015    $17  (this moment i know the second record, and i do not need this record from table_estimate)

Table_estimate
Month, customer, bill
09       MR0015    $17  (i do not need join this record the table_real)
0
Comment
Question by:Rubensv
1 Comment
 
LVL 7

Accepted Solution

by:
Stephen Lappin earned 200 total points
ID: 17898284
I am not quite sure that I totally inderstand, but if you wish to exlcude returning a row from TABLE-ESTIMATE where a corresponing row exists  in TABLE_REAL, then the example below should work.

select month, customer, sum(bill) as bill_month
from table_real
where date_bill between '20060901' and '20060930'
group by month,customer
union
select month, customer, sum(bill) as bill_month
from table_estimate te
where date_bill between '20060901' and '20060930'
where not exists (select null
                          from table_real tr
                         where tr.month = te.month
                         and    tr.customer = te.customer
                         )
group by month,customer
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

972 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