[英]How to deal with 1 level deep nesting limit in Oracle?
I have a query, which should return records from table1
, where number of business day between two dates is greater than 14: 我有一个查询,该查询应从table1
返回记录,其中两个日期之间的营业日数大于14:
select *
from table1
where (select count(*)
from (select rownum n
from table2
where rownum <= sysdate - table1.date_from + 1)
where to_char(table1.date_from + n - 1, 'D') <> 6
and to_char(table1.date_from + n - 1, 'D') <> 7
and not exists (select 1
from holidays
where table1.date_from + n - 1 between holiday_from and holiday_to)) > 14;
First date is selected from table1
( table1.date_from
) and second date is sysdate
. 从table1
( table1.date_from
)中选择第一个日期,第二个日期是sysdate
。 I need to check all days between these days and exclude saturdays, sundays and holidays (from table holidays
). 我需要检查这两天之间的所有天,并排除周六,周日和节假日(从表holidays
)。 The problem is in part: 问题部分在于:
(select rownum n
from table2
where rownum <= sysdate - table1.date_from + 1)
because i can't get there table1.date_from
(1 level deep limit). 因为我无法到达那里table1.date_from
(1级深度限制)。 I tried using CONNECT BY: 我尝试使用CONNECT BY:
select *
from table1
where (select count(*)
from dual
where to_char(table1.date_from + LEVEL - 1, 'D') <> 6
and to_char(table1.date_from + LEVEL - 1, 'D') <> 7
and not exists (select 1
from holidays
where table1.date_from + LEVEL - 1 between holiday_from and holiday_to)
CONNECT BY LEVEL <= sysdate - table1.date_from + 1) > 14;
but i can't use LEVEL
here: 但我不能在这里使用LEVEL
:
and not exists (select 1 from holidays
where table1.date_from + LEVEL - 1 between holiday_from and holiday_to)
Moreover, I can't use function in filter, because of performance issues. 而且,由于性能问题,我不能在过滤器中使用函数。 So, what is the best solution to solve this problem? 那么,解决此问题的最佳解决方案是什么?
UPDATE: 更新:
@imbalind: I really like your approach with starting and descending from SYSDATE and I used it in my solution. @imbalind:我真的很喜欢您从SYSDATE开始和结束的方法,并且在解决方案中使用了它。
@Lalit Kumar B: Using WITH clause solved the problem with 1 level deep limit. @Lalit Kumar B:使用WITH子句可以解决1级深度限制的问题。
I combined hints from your answers and this is my new query (working): 我结合了您的答案中的提示,这是我的新查询(有效):
select *
from table1
where (with counter as (select rownum n
from table2
where rownum <= 40)
select count(*)
from counter
where sysdate - n >= table1.date_from
and to_char(sysdate - n, 'D') <> DECODE('N', 'T', '-1', '6')
and to_char(sysdate - n, 'D') <> DECODE('N', 'T', '-1', '7')
and not exists (select 1
from holidays
where sysdate - n between holiday_from and holiday_to)) > 14;
Thank you very much, I really appreciate your help. 非常感谢您,我非常感谢您的帮助。
My approach would be to generate a fixed number of dates starting from sysdate -1 descending and then work on them. 我的方法是从sysdate -1降序生成固定数量的日期,然后对其进行处理。 In the following snippet I chose 100. Once you got the right date, just use it to filter on table1. 在以下代码段中,我选择了100。一旦您获得了正确的日期,就可以使用它对table1进行过滤。
select dates
from (
select rownum as rn,
dates
from (
select x.dates,
nvl2(h.holiday_from,'T','F') as hd,
decode (to_char(x.dates,'D')-1,6,'T',7,'T','F') as WE
from (
select trunc(sysdate) - rownum as dates
from dual d
connect By rownum <= 100 -- change this number if you plan on having long holidays
) x
left outer join holidays h
on x.dates between h.holiday_fromand h.holiday_to
)
where hd = 'F' and WE = 'F' -- exclude holidays and weekends
)
where rn = 14; -- return the 14th working day from now
because i can't get there table1.date_from (1 level deep limit). 因为我无法到达那里table1.date_from(1级深度限制)。 I tried using CONNECT BY: 我尝试使用CONNECT BY:
You could use subquery factoring, ie WITH clause to avoid the SQL restriction on subquery. 您可以使用子查询分解,即WITH子句来避免SQL对子查询的限制。
Try this query, 试试这个查询,
SELECT *
FROM table1
WHERE (WITH required_dates(d)
AS (SELECT date_from + LEVEL - 1
FROM table1
CONNECT BY LEVEL < = ( SYSDATE - date_from ) + 1)
SELECT Count(*)
FROM required_dates
WHERE To_char(d, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') NOT IN ( 'SAT',
'SUN' )
AND NOT EXISTS (SELECT 1
FROM holidays
WHERE table1.date_from + n - 1 BETWEEN
holiday_from AND holiday_to)) > 14
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.