Question

To clarify the question further...

I have the following Table ORDERS_TAB with some example information:

|Order| Launch_Date |
|  1  |   27-Sep-11 |
|  2  |   29-Sep-11 |
|  3  |   30-Sep-11 |
|  4  |   03-Oct-11 |
|  5  |   28-Oct-11 |
|  8  |   10-Oct-11 |
|  7  |   29-Sep-11 |

I want to select all orders that have a LAUNCH_DATE value within the next three available days including the current day. So, as today is 29-Sep-11 the three available dates from the table would be 29-Sep-11, 30-Sep-11 and 03-Oct-11. Thus the query would return the result:

|Order| Launch_Date |
|  2  |   29-Sep-11 |
|  3  |   30-Sep-11 |
|  4  |   03-Oct-11 |
|  7  |   29-Sep-11 |

So I assume I would need to check what the current date is in the system, search for this date or the next available one and then find the next two available dates after that.. the problem is I have no idea how to do this!

All I am looking for is a simple SELECT statement with some conditions but any help is much appreciated,

Thank-you. :)

Was it helpful?

Solution

SELECT
  *
FROM
(
  SELECT
    DENSE_RANK() OVER (ORDER BY Launch_Date) available_date_index,
    *
  FROM
    ORDERS_TAB
  WHERE
    Launch_Date >= TRUNC(Sysdate)
)
WHERE
  available_date_index <= 3


EDIT As there is confusion about how DENSE_RANK() works, here is the OP's example...

|Order| Launch_Date |    Available_Date_Index
|  1  |   27-Sep-11 | <excluded by WHERE clause>
|  2  |   29-Sep-11 |             1
|  3  |   30-Sep-11 |             2
|  4  |   03-Oct-11 |             3
|  5  |   28-Oct-11 |             5
|  8  |   10-Oct-11 |             4
|  7  |   29-Sep-11 |             1

This means that the returned Orders will be 2,3,4,7. Exactly as described by the OP.

OTHER TIPS

select * from ORDERS_TAB where Launch_Date in
   (select Launch_Date from
          (select distinct Launch_Date from ORDERS_TAB
                  where Launch_Date >= '2011-09-29' order by Launch_Date)
    WHERE rownum <= 3)
select * from order_tab where launch_date in
(    
  select launchdate
    from
    (
      select launchdate from order_tab
      where launch_date >= SysDate -- Check this
      order by launch_date
    )  where rownum <= 3
)

How does that look ?

Edit: As the comments point out, the following doesn't work maybe go for

SELECT * FROM ORDERS_TAB WHERE LaunchDate IN (
  SELECT TOP(3) LaunchDate FROM ORDERS_TAB ((possibly WHERE LaunchDate > GETDATE()?))
  ORDER BY LaunchDate DESC )

Try this:

SELECT * FROM ORDERS_TAB WHERE sysdate - launch_date <= 3

SELECT * FROM ORDERS_TAB where Launch_Date between '29-sep-11' and ADDDATE('2007-09-01', 3); try this, hope this help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top