Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

query

Posted on 2005-05-02
5
Medium Priority
?
551 Views
Last Modified: 2008-03-10
I have two tables i want to retrive those rows which matches as well as not matching query. I am doing outer join.

RRS_SPK_UNALLOC_BUD_STG --- > 48 rows
RRS_BUDGET_TGT  --- > 22495

It should return me 22543 . It is returning me those rows. But RRS_SPK_UNALLOC_BUD_STG has 4 rows not matching with other tables . It shows me duplicate value.

I do following query.

SELECT A.DEPARTMENT,A.EXPENSE_CODE,NVL(A.BUDGET_AMOUNT,0),
A.YEAR_END_DATE,NVL(B.BUDGET,0), (NVL(A.BUDGET_AMOUNT,0) + NVL(B.BUDGET,0)) TOTAL FROM
RRS_BUDGET_TGT A,
RRS_SPK_UNALLOC_BUD_STG B
WHERE A.DEPARTMENT = B.DEPT_ID (+)
AND   A.EXPENSE_CODE = B.SUB_ACCOUNT (+)
AND   A.YEAR_END_DATE = B.END_OF_YEAR_DATE (+)
UNION
select C.dept_id,C.sub_account,nvl(c.budget,0),c.end_of_year_date,nvl(c.budget,0),
(nvl(c.budget,0) + nvl(c.budget,0)) Total from rrs_spk_unalloc_bud_stg C where  exists
(
SELECT B.DEPT_ID,A.YEAR,B.SUB_ACCOUNT,NVL(B.BUDGET,0),
B.END_OF_YEAR_DATE,NVL(B.BUDGET,0), (NVL(A.BUDGET_AMOUNT,0) + NVL(B.BUDGET,0)) TOTAL FROM
RRS_BUDGET_TGT A,
RRS_SPK_UNALLOC_BUD_STG B
WHERE A.DEPARTMENT  = B.DEPT_ID
AND   A.EXPENSE_CODE = B.SUB_ACCOUNT
AND   A.YEAR_END_DATE = B.END_OF_YEAR_DATE
);

I just want to see 22495 rows + 4 rows from RRS_SPK_UNALLOC_BUD_STG table which are not matched.
0
Comment
Question by:frinpd
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:izblank
ID: 13910009
I take it the second part of the query is for not matched rows.  If that's true, try changing the condition to "not exists"

SELECT A.DEPARTMENT,A.EXPENSE_CODE,NVL(A.BUDGET_AMOUNT,0),
A.YEAR_END_DATE,NVL(B.BUDGET,0), (NVL(A.BUDGET_AMOUNT,0) + NVL(B.BUDGET,0)) TOTAL FROM
RRS_BUDGET_TGT A,
RRS_SPK_UNALLOC_BUD_STG B
WHERE A.DEPARTMENT = B.DEPT_ID (+)
AND   A.EXPENSE_CODE = B.SUB_ACCOUNT (+)
AND   A.YEAR_END_DATE = B.END_OF_YEAR_DATE (+)
UNION
select C.dept_id,C.sub_account,nvl(c.budget,0),c.end_of_year_date,nvl(c.budget,0),
(nvl(c.budget,0) + nvl(c.budget,0)) Total from rrs_spk_unalloc_bud_stg C where  NOT EXISTS
(
SELECT B.DEPT_ID,A.YEAR,B.SUB_ACCOUNT,NVL(B.BUDGET,0),
B.END_OF_YEAR_DATE,NVL(B.BUDGET,0), (NVL(A.BUDGET_AMOUNT,0) + NVL(B.BUDGET,0)) TOTAL FROM
RRS_BUDGET_TGT A,
RRS_SPK_UNALLOC_BUD_STG B
WHERE A.DEPARTMENT  = B.DEPT_ID
AND   A.EXPENSE_CODE = B.SUB_ACCOUNT
AND   A.YEAR_END_DATE = B.END_OF_YEAR_DATE
);
0
 

Author Comment

by:frinpd
ID: 13910030
The Second Part with Not Exists does not return any rows. It's only A.Expense_code and B.sub_account that does not match between two tables for 4 rows.
0
 
LVL 6

Expert Comment

by:izblank
ID: 13910064
Could you post a small sample of data in the two tables involved and explain in more detail what is considered a match and what is not?
0
 
LVL 6

Accepted Solution

by:
izblank earned 1000 total points
ID: 13910097
OK, I got it.  change the second part to this:

select C.dept_id,C.sub_account,nvl(c.budget,0),c.end_of_year_date,nvl(c.budget,0),
(nvl(c.budget,0) + nvl(c.budget,0)) Total from rrs_spk_unalloc_bud_stg C where  NOT EXISTS
(
SELECT *
FROM
RRS_BUDGET_TGT A
WHERE A.DEPARTMENT  = C.DEPT_ID
AND   A.EXPENSE_CODE = C.SUB_ACCOUNT
AND   A.YEAR_END_DATE = C.END_OF_YEAR_DATE
);
0
 

Author Comment

by:frinpd
ID: 13910193
You are the man ! Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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 configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

564 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