Solved

Oracle SqlPlus Conditional Union

Posted on 2006-11-08
1
2,746 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:
lappins earned 50 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Often, when running a query with joins, the results show up "duplicates", and often, those duplicates can be "eliminated" in the results using DISTINCT, for example. Using DISTINCT is simple: just add it after the SELECT keyword, an…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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