tags:

views:

224

answers:

5
+7  A: 

Warning: Not a DBA by any means. ;)

But, a quick, untested stab at it:

SELECT min(CLNDR_DATE) FROM [TABLE]
WHERE (EFFECTIVE_DATE IS NOT NULL)
  AND (CLNDR_DATE > (
    SELECT max(CLNDR_DATE) FROM [TABLE] WHERE EFFECTIVE_DATE IS NULL
  ))

Assuming you want the first CLNDR_DATE with EFFECTIVE_DATE after the last without.

If you want the first with after the first without, change the subquery to use min() instead of max().

Jonathan Lonowski
A: 

The first result from this recordset is what you're looking for. Depending on your Database, you may be able to only return this row by using LIMIT, or TOP

SELECT CLNDR_DATE 
FROM TABLE
WHERE CLNDR_DATE > (SELECT MAX(CLNDR_DATE)
                    FROM TABLE 
                    WHERE EFFECTIVE_DATE IS NOT NULL)
ORDER BY CLNDR_DATE
Wayne
A: 

When you are in Oracle environment you can use analytical functions (http://www.orafaq.com/node/55), which are very powerful tools to do kind of queries you asking for.

Using standard SQL I think it is impossible, but maybe some SQL gurus will show up some nice solution.

A: 

Cool, Looks like First answer should works.

+1  A: 

Using Oracle's analytic function (untested)

select *
from
(
  select 
    clndr_date, 
    effective_date, 
    lag(clndr_date, 1, null) over (order by clndr_date desc) prev_clndr_date
  from table
)
where effective_date is null

The lag(clndr_date, 1, null) over (order by clndr_date desc) returns the previous clndr_date, or use null if this is the first row.

(edit: fixed order)

jop
over (order by clndr_date DESC)does the job.But yes, too slow :-(
Sergey Stadnik
The advantage of this is that it will show you all the gaps, not just the latest, this may or may not be what the OP wants, most likely not by the look of it.
Matthew Watson