查询2023-07-03和2023-08-06之间的所有日期(从开始日期起5周)。

huangapple go评论121阅读模式
英文:

How to query all the dates between 2023-07-03 and 2023-08-06 (5 weeks from the start date)?

问题

以下是翻译好的部分:

以下数据库已创建:

`INSERT INTO T4S_BRANCH_DATA (BR_NAME, BR_START, BR_COURSE_LN) VALUES ('COMMUNITY', 
TO_DATE('2023-07-03', 'YYYY-MM-DD'), 5);
INSERT INTO T4S_BRANCH_DATA (BR_NAME, BR_START, BR_COURSE_LN) VALUES ('HAF', TO_DATE('2023-07- 
03', 'YYYY-MM-DD'), 4);
INSERT INTO T4S_BRANCH_DATA (BR_NAME, BR_START, BR_COURSE_LN) VALUES ('HUBB', TO_DATE('2023- 
07-03', 'YYYY-MM-DD'), 4);

查询尝试:

`select BR_START + level - 1 DT
 from   t4s_branch_data where BR_NAME = 'COMMUNITY'
 connect by level <= (
 (BR_START + BR_COURSE_LN*7-1) - BR_START + 1);

但是,这会打印出 5,000 行重复的数据。
英文:

Below database created:

`INSERT INTO T4S_BRANCH_DATA (BR_NAME, BR_START, BR_COURSE_LN) VALUES ('COMMUNITY', 
TO_DATE('2023-07-03', 'YYYY-MM-DD'), 5);
INSERT INTO T4S_BRANCH_DATA (BR_NAME, BR_START, BR_COURSE_LN) VALUES ('HAF', TO_DATE('2023-07- 
03', 'YYYY-MM-DD'), 4);
INSERT INTO T4S_BRANCH_DATA (BR_NAME, BR_START, BR_COURSE_LN) VALUES ('HUBB', TO_DATE('2023- 
07-03', 'YYYY-MM-DD'), 4);

Query tried:

`select BR_START + level - 1 DT
 from   t4s_branch_data where BR_NAME = 'COMMUNITY'
 connect by level <= (
 (BR_START + BR_COURSE_LN*7-1) - BR_START + 1);

However, this prints 5,000 rows of repeated data

答案1

得分: 1

在应用CONNECT BY子句之前,您需要限制要使用的行:

with comm as
  (select * from t4s_branch_data where BR_NAME = 'COMMUNITY')
select BR_START + level - 1 DT
 from  comm
 connect by level <= (
 (BR_START + BR_COURSE_LN*7-1) - BR_START + 1);

或者,可以使用内联视图代替WITH子句。

英文:

you need to limit the rows you want to use BEFORE you apply the connect by clause:

with comm as
  (select * from t4s_branch_data where BR_NAME = &#39;COMMUNITY&#39;)
select BR_START + level - 1 DT
 from  comm
 connect by level &lt;= (
 (BR_START + BR_COURSE_LN*7-1) - BR_START + 1);

or use an inline view instead of a WITH clause

答案2

得分: 0

在连接子查询中有(超过足够的)级别的一个选项,限制在Where子句中生成的日期数量
SELECT        t.BR_START + l.LVL "DT"
FROM          t4s_branch_data t
INNER JOIN    ( SELECT LEVEL - 1 "LVL" From Dual Connect By LEVEL <= 60 ) l ON(1 = 1)
WHERE         t.BR_NAME = 'COMMUNITY' And l.LVL <= ( (t.BR_START + t.BR_COURSE_LN*7-1) - t.BR_START )
英文:

One of the options is to have (more than enough) Levels in joined subquery limiting the number of dates generated in Where clause:

SELECT        t.BR_START + l.LVL &quot;DT&quot;
FROM          t4s_branch_data t
INNER JOIN    ( SELECT LEVEL - 1 &quot;LVL&quot; From Dual Connect By LEVEL &lt;= 60 ) l ON(1 = 1)
WHERE         t.BR_NAME = &#39;COMMUNITY&#39; And l.LVL &lt;= ( (t.BR_START + t.BR_COURSE_LN*7-1) - t.BR_START )

huangapple
  • 本文由 发表于 2023年2月27日 08:32:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定