我正在研究一个类似于未来约会的场景。
我有一张桌子,跟这个差不多
ProductID ProductStatus EffectiveFromDate EffectiveToDate CancelledIndicator
----------- ------------- ----------------- --------------- ------------------
345 A 7/7/2016 (null) 1
345 S 7/7/2016 11/7/2016 (null)
345 A 12/7/2016 (null) (null)我需要获取当前的日期产品,如果我找到一个取消的指示符,这意味着它不再是活跃的,如果他们是两行,一与未来的日期状态。根据上面的表,如果检查efd < sysdate和etd为null,就会得到最新的记录。但要获得当前的活动状态,这正是我需要实施的情况。
我需要检查是否系统是b/w,efd和etd的旧记录,如果不是,我需要采取最新的记录,这将是目前的状态。
我有个疑问是谁做的
但问题是当我检查
sysdate between efd and etd where etd can be null most of the time.发布于 2016-07-12 09:51:02
关于如何处理它的一些示例:
设置:
SQL> create table testNull (id, startDate, endDate) as
2 (
3 select 1, null, sysdate + 1 from dual union all
4 select 2, sysdate -1, sysdate + 1 from dual union all
5 select 3, sysdate -1, null from dual union all
6 select 4, sysdate -3, sysdate - 1 from dual
7 );
Table created.而不处理NULL:
SQL> select *
2 from testNull
3 where sysdate between startDate and endDate ;
ID STARTDATE ENDDATE
---------- --------- ---------
2 11-LUG-16 13-LUG-16与COALESCE:
SQL> select *
2 from testNull
3 where sysdate between startDate and coalesce(endDate, sysdate);
ID STARTDATE ENDDATE
---------- --------- ---------
2 11-LUG-16 13-LUG-16
3 11-LUG-16带有布尔逻辑的:
SQL> select *
2 from testNull
3 where sysdate >= startDate
4 and ( endDate is null or sysdate <= endDate);
ID STARTDATE ENDDATE
---------- --------- ---------
2 11-LUG-16 13-LUG-16
3 11-LUG-16https://stackoverflow.com/questions/38325177
复制相似问题