如何在Oracle中获取当前生效日期?

时间:2011-10-12 11:48:34

标签: sql oracle

我有一张如下表:

TID TName EffectiveDate
1 A 2011-7-1
2 A 2011-8-1
3 A 2011-9-1
4 A 2011-10-1
5 B 2011-8-1
6 B 2011-9-1
7 B 2011-10-1
8 C 2011-9-1
etc...

如果今天是2011-9-10,我希望查询结果如下:

TID TName EffectiveDate Status
1 A 2011-7-1 Invalid
2 A 2011-8-1 Invalid
3 A 2011-9-1 Valid
4 A 2011-10-1 Inactive
5 B 2011-8-1 Invalid
6 B 2011-9-1 Valid
7 B 2011-10-1 Inactive
8 C 2011-9-1 Valid

如果今天是2011-10-2,查询结果将如下:

TID TName EffectiveDate Status
1 A 2011-7-1 Invalid
2 A 2011-8-1 Invalid
3 A 2011-9-1 Invalid
4 A 2011-10-1 Valid
5 B 2011-8-1 Invalid
6 B 2011-9-1 InValid
7 B 2011-10-1 Valid
8 C 2011-9-1 Valid

查询结果将添加另一个名为“Status”的列,状态值基于今天的值,并将其与列effectivedate进行比较。最大有效日期将显示为“有效”状态。如果今天的价值在两个记录之间,则后者是“非活动”状态。

如何编写语句以在oracle中获得此结果?

提前致谢。

2 个答案:

答案 0 :(得分:3)

尝试:

select TID,
       TName,
       EffectiveDate,
       decode(sign(EffectiveDate - (select max(T2.EffectiveDate) 
                                    from MyTable T2
                                    where T1.Tname=T2.Tname and 
                                          T2.EffectiveDate <= sysdate)),
              -1,'Invalid',
              0,'Valid',
              'Inactive') Status
from MyTable T1

答案 1 :(得分:2)

不要尝试,但要亲自看看:-)
只有一个表访问。

你的桌子:

SQL> create table mytable (tid,tname,effectivedate)
  2  as
  3  select 1, 'A', date '2011-07-01' from dual union all
  4  select 2, 'A', date '2011-08-01' from dual union all
  5  select 3, 'A', date '2011-09-01' from dual union all
  6  select 4, 'A', date '2011-10-01' from dual union all
  7  select 5, 'B', date '2011-08-01' from dual union all
  8  select 6, 'B', date '2011-09-01' from dual union all
  9  select 7, 'B', date '2011-10-01' from dual union all
 10  select 8, 'C', date '2011-09-01' from dual
 11  /

Table created.

你的两个测试用例:

SQL> var TODAY varchar2(10)
SQL> exec :TODAY := '2011-09-10'

PL/SQL procedure successfully completed.

SQL> select tid
  2       , tname
  3       , effectivedate
  4       , case
  5         when  to_date(:TODAY,'yyyy-mm-dd') >= effectivedate
  6           and to_date(:TODAY,'yyyy-mm-dd') < next_effectivedate
  7         then
  8           'Valid'
  9         when to_date(:TODAY,'yyyy-mm-dd') >= effectivedate
 10         then
 11           'Invalid'
 12         else
 13           'Inactive'
 14         end status
 15    from ( select tid
 16                , tname
 17                , effectivedate
 18                , lead(effectivedate,1,date '9999-12-31') over (partition by tname order by effectivedate) next_effectivedate
 19             from mytable
 20         )
 21  /

       TID T EFFECTIVEDATE       STATUS
---------- - ------------------- --------
         1 A 01-07-2011 00:00:00 Invalid
         2 A 01-08-2011 00:00:00 Invalid
         3 A 01-09-2011 00:00:00 Valid
         4 A 01-10-2011 00:00:00 Inactive
         5 B 01-08-2011 00:00:00 Invalid
         6 B 01-09-2011 00:00:00 Valid
         7 B 01-10-2011 00:00:00 Inactive
         8 C 01-09-2011 00:00:00 Valid

8 rows selected.

SQL> exec :TODAY := '2011-10-02'

PL/SQL procedure successfully completed.

SQL> select tid
  2       , tname
  3       , effectivedate
  4       , case
  5         when  to_date(:TODAY,'yyyy-mm-dd') >= effectivedate
  6           and to_date(:TODAY,'yyyy-mm-dd') < next_effectivedate
  7         then
  8           'Valid'
  9         when to_date(:TODAY,'yyyy-mm-dd') >= effectivedate
 10         then
 11           'Invalid'
 12         else
 13           'Inactive'
 14         end status
 15    from ( select tid
 16                , tname
 17                , effectivedate
 18                , lead(effectivedate,1,date '9999-12-31') over (partition by tname order by effectivedate) next_effectivedate
 19             from mytable
 20         )
 21  /

       TID T EFFECTIVEDATE       STATUS
---------- - ------------------- --------
         1 A 01-07-2011 00:00:00 Invalid
         2 A 01-08-2011 00:00:00 Invalid
         3 A 01-09-2011 00:00:00 Invalid
         4 A 01-10-2011 00:00:00 Valid
         5 B 01-08-2011 00:00:00 Invalid
         6 B 01-09-2011 00:00:00 Invalid
         7 B 01-10-2011 00:00:00 Valid
         8 C 01-09-2011 00:00:00 Valid

8 rows selected.

的问候,
罗布。