Question : Date range issue

Hi,

I have a table where I track orders on a monthly basis while they remain open with the scope limited to the beginning of 2009.  I have another table that tracks order status changes during the order life cycle.  Some of the status changes for an order may have occurred prior to 2009.

The order table::
Date       Order   State
-----        --------  ------
2009/01 OrderA Open
2009/02 OrderA Open
2009/03 OrderA Open
2009/04 OrderA Open
2009/05 OrderA Open
2009/06 OrderA Open
2009/07 OrderA Open

The status table:
Order    Status   Status Date
-------    -------    --------------
OrderA Status1 2008/03/05
OrderA Status2 2009/02/01
OrderA Status3 2009/05/10

How would the query look to get the following?

Date       Order   State  Status
-----        --------  ------  --------
2009/01 OrderA Open Status1
2009/02 OrderA Open Status2
2009/03 OrderA Open Status2
2009/04 OrderA Open Status2
2009/05 OrderA Open Status3
2009/06 OrderA Open Status3
2009/07 OrderA Open Status3

I've tried queries like below but I'm missing something as I'm not getting the desired output.

select order_table.date,
          order_table.order,
          order_table.state,
          status_table.status
from order_table,
        status_table
where order_table.order = status_table.order
           and status_table.status_date < order_table.date

Thanks in advance.

Answer : Date range issue

something like this:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
select * 
from (
select order_table.date,
          order_table.order,
          order_table.state,
          status_table.status,
     row_number() over (partition by status_table.order order by status_table.status_date desc ) rn 
from order_table,
        status_table
where order_table.order = status_table.order
           and status_table.status_date < order_table.date
) sq
where sq.rn = 1
Random Solutions  
 
programming4us programming4us