I'm not sure if it is appropriate to call the problem as "previous valid value". The thing is like below:
I have a table "A":
create table A (
name varchar(16),
performanceDate date,
value int
);
Both name and PerformanceDate are primary key.
There is a process that will insert data for each user every day. So, the data will look something like below:
select * from A;
|------+-----------------+-------|
| name | performanceDate | value |
|------+-----------------+-------|
| Joe | 2012-05-18 | null |
| Joe | 2012-05-17 | 2 |
| Joe | 2012-05-16 | null |
| Joe | 2012-05-15 | null |
| Joe | 2012-05-14 | 3 |
|------+-----------------+-------|
Presently, I want to get a result set with preformanceDate between 2012-05-16 and 2012-05-18 If the value for current date is null, it should be replaced to the previous vaild value. For example, the value of 2012-05-16 is null, the first valid value before 2012-05-16 is 3
on 2012-05-14. So that, the result would like below:
|------+-----------------+-------|
| name | performanceDate | value |
|------+-----------------+-------|
| Joe | 2012-05-18 | 2 |
| Joe | 2012-05-17 | 2 |
| Joe | 2012-05-16 | 3 |
|------+-----------------+-------|
So far, I plan to insert the data into a temp table first (because the table "A" is read only for me), then update the value one by one. But this way is very slow. Do you have any idea about this?
You can use LATERAL JOIN, DB2 is not yet on SQLFiddle, following is its equivalent in SQL Server. LATERAL is equivalent to SQL Server's APPLY:
Data:
Output:
Live test: http://www.sqlfiddle.com/#!6/e0158/8
Base on IBM documentation.. http://publib.boulder.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Fdb2%2Frbafztabref.htm
..,I wish this is that:
Interesting metrics on CROSS APPY/OUTER APPLY: http://explainextended.com/2009/07/16/inner-join-vs-cross-apply/
Another example of OUTER APPLY's use: http://www.ienablemuch.com/2012/04/outer-apply-walkthrough.html