Oracle - 仅返回最早日期或未来日期

Pat*_*rio 2 sql oracle date

假设我有一个"机会"表(机会可以有多个截止日期),如下所示:

机会

id    opp_id    deadline
---------------------------
1     40        20-Jan-2016
2     40        13-Jan-2016
3     40        20-Apr-2016
4     40        13-Apr-2016
5     73        29-Sep-2015
6     95        11-Dec-2016
Run Code Online (Sandbox Code Playgroud)

我们假设今天的日期是15-Jan-2016,应该返回的是:

id    opp_id    deadline
---------------------------
6     95        11-Dec-2016
Run Code Online (Sandbox Code Playgroud)

但如果我们假设今天的日期是16-Dec-2015,那么我希望收到的行是:

id    opp_id    deadline
---------------------------
1     40        20-Jan-2016
2     40        13-Jan-2016
3     40        20-Apr-2016
4     40        13-Apr-2016
6     95        11-Dec-2016
Run Code Online (Sandbox Code Playgroud)

基本上,逻辑是在今天或未来的所有截止日期中获得这些机会.这些是我仍然可以申请的机会,因为我仍然可以在截止日期前完成.

我该怎么做呢?

编辑

我想我也应该澄清一点,如果机会的最后期限已经过去了,我不希望得到这个特定机会的剩余截止日期.基本上如果最早的截止日期已经过去,我不想为这个机会找回任何东西.

Gor*_*off 5

一种方法使用分析函数:

select o.*
from (select o.*, min(deadline) over (partition by opp_id) as min_deadline
      from opportunities o
     ) o
where min_deadline >= sysdate;
Run Code Online (Sandbox Code Playgroud)

你也可以这样做not exists;

select o.*
from opportunities o
where not exists (select 1
                  from opportunities o2
                  where o2.opp_id = o.opp_id and o2.deadline < sysdate
                 );
Run Code Online (Sandbox Code Playgroud)