我对oracle完全陌生。我有一张桌子,上面存储着工作日。我想在满足输入条件的情况下得到第5个工作日(第5行)。例如,请考虑下表
BUSINESS DAY
8/06/2004 12:00:00 AM
8/07/2004 12:00:00 AM
8/08/2004 12:00:00 AM
8/09/2004 12:00:00 AM
8/10/2004 12:00:00 AM
8/13/2004 12:00:00 AM
8/14/2004 12:00:00 AM
8/15/2004 12:00:00 AM
8/16/2004 12:00:00 AM
在这里,我的输入日期为2004年8月8日12:00:00 AM,我的输出应为2004年8月15日12:00:00 AM。请帮助构建查询。请注意,根据要求,我可能还需要过去的日期。
我还提到了其他栈问题和答案。但是似乎它们都不符合我的问题。
答案 0 :(得分:2)
演示表:
create table business_days (busday) as
select date '2004-08-06' from dual union
select date '2004-08-07' from dual union
select date '2004-08-08' from dual union
select date '2004-08-09' from dual union
select date '2004-08-10' from dual union
select date '2004-08-13' from dual union
select date '2004-08-14' from dual union
select date '2004-08-15' from dual union
select date '2004-08-16' from dual;
一种方法:
select busday, t_plus_5
from ( select busday
, lead(busday,5) over (order by busday) as t_plus_5
from business_days
where busday >= date '2004-08-08' )
where busday = date '2004-08-08';
或者这(需要Oracle 12.1或更高版本):
select busday
, ( select d5.busday from business_days d5
where d5.busday >= d1.busday
order by busday
offset 5 rows fetch first row only ) as t_plus_5
from business_days d1
where busday = date '2004-08-08';
答案 1 :(得分:1)
这是一个选择:订购业务日期(使用ROW_NUMBER
分析函数),然后对其应用简单的算术运算(即加5天,或减去一些天):
SQL> with test (busday) as
2 (select date '2004-08-06' from dual union
3 select date '2004-08-07' from dual union
4 select date '2004-08-08' from dual union
5 select date '2004-08-09' from dual union
6 select date '2004-08-10' from dual union
7 select date '2004-08-13' from dual union
8 select date '2004-08-14' from dual union
9 select date '2004-08-15' from dual union
10 select date '2004-08-16' from dual
11 ),
12 inter as
13 (select busday, row_number() over (order by busday) rn
14 from test
15 )
16 select i.busday
17 from inter i
18 where i.rn = (select i1.rn + 5 from inter i1
19 where i1.busday = date '&par_busday'
20 );
Enter value for par_busday: 2004-08-08
BUSDAY
----------
2004-08-15
SQL>