Advertisement

01.25.2008 at 02:13PM PST, ID: 23112551
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6

Fill in NULL values in Outer Join

Asked by larrydare in Oracle 9.x

Tags: , , ,

Problem Statement:
I am performing an outer join between 2 tables.    On the rows where are no matching values,  NULLs are in the column.   All this is expected.  

Now what I would like to happen is that instead of a NULL,  the previous value in the column is used.

Example
Table 1  - CHANGEDATA
Input_month |  Team_Name | ChangeNumber
----------------------------------------
1/1/2008            CC          12
2/1/2008            CC         3
3/1/2008            CC         0
4/1/2008            CC         0
5/1/2008            CC         4
6/1/2008            CC         3
7/1/2008            CC         0
8/1/2008            CC         2
9/1/2008            CC         4
10/1/2008           CC        10
11/1/2008           CC         0
12/1/2008           CC         0

Table 2 - BASEDATA
Input_month | Team_Name | Baseline
----------------------------------------
1/1/2008            CC       100
4/1/2008            CC       111
6/1/2008            CC       120
10/1/2008           CC       125

Doing an outer join yields the following
Input_month |  Team_Name | ChangeNumber | Baseline
--------------------------------------------------
1/1/2008            CC          12            100
2/1/2008            CC         3
3/1/2008            CC         0
4/1/2008            CC         0            111
5/1/2008            CC         4
6/1/2008            CC         3            120
7/1/2008            CC         0
8/1/2008            CC         2
9/1/2008            CC         4
10/1/2008           CC        10            125
11/1/2008           CC         0
12/1/2008           CC         0


Now here is what I would like:
Input_month |  Team_Name | ChangeNumber | Baseline
--------------------------------------------------
1/1/2008            CC          12            100
2/1/2008            CC         3            100
3/1/2008            CC         0            100
4/1/2008            CC         0            111
5/1/2008            CC         4            111
6/1/2008            CC         3            120
7/1/2008            CC         0            120
8/1/2008            CC         2            120
9/1/2008            CC         4            120
10/1/2008           CC        10            125
11/1/2008           CC         0            125
12/1/2008           CC         0            125

Where if Baseline is NULL, I would like the previous non null value.

NON WORKING SOLUTION.
My original solution was to use a User Defined Function to return the Baseline number
So the query would look like this
select a.input_month, a.Team_name, a.ChangeNumber,
     NVL(b.baseline, LastValue(a.input_month, a.Team_Name)) "Baseline"
from CHANGEDATA a, BASEDATA b
where a.input_month = b.input_month (+)
and a.team_name = b.team_name (+)

This query only yielded one row,  the first one.  
The problem is that the User Defined Function, LastValue  querys the table.  If LastValue just returns a hardcoded value,  I get all the rows back,  but the Baseline value does not show the last value.

The following is the LastValue Function:
create or replace function lastvalue(nmonth date,nteam varchar2) return number
as
newbaseline number;
begin
select baseline into newbaseline
from
(select baseline, max(input_month)
from
BASEDATA
where input_month <= nmonth
and TeamName = nteam
group by baseline);
return newbaseline;
end lastvalue;
/

If you run this function from a single row select,   it does return the proper value,  but when you expect multiple rows,   I only get one.


Question:
Is there a different SQL syntax that will give me what I want?
OR
What am I doing wrong in LastValue definition that is causing it to return only one row?


Example Data Creation Statement:
CREATE TABLE CHANGEDATA
(INPUT_MONTH DATE,
 TEAM_NAME VARCHAR2(10),
 CHANGENUMBER NUMBER)
/
INSERT INTO CHANGEDATE VALUES (TO_DATE('JAN-01-2008','MON-DD-YYYY'),'CC',12);
INSERT INTO CHANGEDATE VALUES (TO_DATE('FEB-01-2008','MON-DD-YYYY'),'CC',3);
INSERT INTO CHANGEDATE VALUES (TO_DATE('MAR-01-2008','MON-DD-YYYY'),'CC',0);
INSERT INTO CHANGEDATE VALUES (TO_DATE('APR-01-2008','MON-DD-YYYY'),'CC',0);
INSERT INTO CHANGEDATE VALUES (TO_DATE('MAY-01-2008','MON-DD-YYYY'),'CC',4);
INSERT INTO CHANGEDATE VALUES (TO_DATE('JUN-01-2008','MON-DD-YYYY'),'CC',3);
INSERT INTO CHANGEDATE VALUES (TO_DATE('JUL-01-2008','MON-DD-YYYY'),'CC',0);
INSERT INTO CHANGEDATE VALUES (TO_DATE('AUG-01-2008','MON-DD-YYYY'),'CC',2);
INSERT INTO CHANGEDATE VALUES (TO_DATE('SEP-01-2008','MON-DD-YYYY'),'CC',4);
INSERT INTO CHANGEDATE VALUES (TO_DATE('OCT-01-2008','MON-DD-YYYY'),'CC',10);
INSERT INTO CHANGEDATE VALUES (TO_DATE('NOV-01-2008','MON-DD-YYYY'),'CC',0);
INSERT INTO CHANGEDATE VALUES (TO_DATE('DEC-01-2008','MON-DD-YYYY'),'CC',0);

CREATE TABLE BASEDATA
(INPUT_MONTH DATE,
TEAM_NAME VARCHAR2(10),
BASELINE NUMBER)
/
INSERT INTO BASEDATA VALUES (TO_DATE('JAN-01-2008','MON-DD-YYYY'),'CC',100);
INSERT INTO BASEDATA VALUES (TO_DATE('APR-01-2008','MON-DD-YYYY'),'CC',111);
INSERT INTO BASEDATA VALUES (TO_DATE('JUN-01-2008','MON-DD-YYYY'),'CC',120);
INSERT INTO BASEDATA VALUES (TO_DATE('OCT-01-2008','MON-DD-YYYY'),'CC',125;

NORMAL OUTER JOIN
select a.input_month, a.Team_name, a.ChangeNumber,b.baseline
from CHANGEDATA a, BASEDATA b
where a.input_month = b.input_month (+)
and a.team_name = b.team_name (+)

Start Free Trial
[+][-]01.25.2008 at 02:57PM PST, ID: 20747105

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.25.2008 at 02:58PM PST, ID: 20747118

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.25.2008 at 03:18PM PST, ID: 20747278

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Oracle 9.x
Tags: Oracle, SQL, Version 9, Fill in NULL values in outer join
Sign Up Now!
Solution Provided By: sdstuber
Participating Experts: 1
Solution Grade: A
 
 
[+][-]01.25.2008 at 03:20PM PST, ID: 20747300

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628