Solved

Oracle SqlPlus Conditional Union

Posted on 2006-11-08
1
2,768 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
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need Help Entering Data For Personnel in Excel Workbook 12 92
Client backup and data retention? 3 54
SQL Update Query 23 93
Update cached table in H2 database 6 43
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
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…

911 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

22 Experts available now in Live!

Get 1:1 Help Now