在月末中断相等的期间

Interrupt equal periods at the end of the month

甲骨文数据库:SQL_FIDDLE

这是关于计数和每周周期的开始:

Table_1 列出计划及其开始日期。

Table_2 列出 Table_1.

中的时期和相应的计划

select

select 
t2.PERIOD
 ,'WEEK_NUMBER ' || to_char(t1.START_DATE +((t2.PERIOD-1) * 7), 'iw') Week
,to_char(t1.START_DATE + ((t2.PERIOD-1) * 7), 'YYYY-MM-DD') Period_Start
from TABLE_1 t1
join
TABLE_2 t2
on t1.PLAN = t2.PLAN

打印以下内容:

+--------+----------------+--------------+
| PERIOD |      WEEK      | PERIOD_START |
+--------+----------------+--------------+
|      1 | WEEK_NUMBER 18 | 2020-05-01   |
|      2 | WEEK_NUMBER 19 | 2020-05-08   |
|      3 | WEEK_NUMBER 20 | 2020-05-15   |
|      4 | WEEK_NUMBER 21 | 2020-05-22   |
|      5 | WEEK_NUMBER 22 | 2020-05-29   |
|      6 | WEEK_NUMBER 23 | 2020-06-05   |
|      7 | WEEK_NUMBER 24 | 2020-06-12   |
|      8 | WEEK_NUMBER 25 | 2020-06-19   |
+--------+----------------+--------------+

问题: 每个时期应有 7 天,但有一个月休息的时期除外。如果一期有月休,则应到该月的最后一天,其余的作为下个月的新期继续。应调整周数和期间数。

示例:

+--------+----------------+--------------+
| PERIOD |      WEEK      | PERIOD_START |
+--------+----------------+--------------+
|      1 | WEEK_NUMBER 18 | 2020-05-01   |
|      2 | WEEK_NUMBER 19 | 2020-05-08   |
|      3 | WEEK_NUMBER 20 | 2020-05-15   |
|      4 | WEEK_NUMBER 21 | 2020-05-22   |
|      5 | WEEK_NUMBER 22 | 2020-05-29   |< --- period part before new month
|      6 | WEEK_NUMBER 23 | 2020-06-01   |< --- period part after new month
|      7 | WEEK_NUMBER 23 | 2020-06-05   |
|      8 | WEEK_NUMBER 24 | 2020-06-12   |
|      9 | WEEK_NUMBER 25 | 2020-06-19   |
+--------+----------------+--------------+

这里推荐哪个程序?

我添加了另一个 SQL_FIDDLE 和附加句点:

+--------+----------------+--------------+
| PERIOD |      WEEK      | PERIOD_START |
+--------+----------------+--------------+
|      1 | WEEK_NUMBER 23 | 2020-06-01   |
|      2 | WEEK_NUMBER 24 | 2020-06-08   |
|      3 | WEEK_NUMBER 25 | 2020-06-15   |
|      4 | WEEK_NUMBER 26 | 2020-06-22   |
|      5 | WEEK_NUMBER 22 | 2020-05-29   |< --- period part before new month
|      6 | WEEK_NUMBER 23 | 2020-06-01   |< --- period part after new month (Week_Number has changed)
|      6 | WEEK_NUMBER 28 | 2020-07-06   |
|      7 | WEEK_NUMBER 29 | 2020-07-13   |
|      8 | WEEK_NUMBER 30 | 2020-07-20   |
|      9 | WEEK_NUMBER 31 | 2020-07-27   |< --- period part before new month
|     10 | WEEK_NUMBER 31 | 2020-08-01   |< --- period part afternew month (Week_Number has not changed)
|     11 | WEEK_NUMBER 32 | 2020-08-03   |
|     12 | WEEK_NUMBER 33 | 2020-08-10   |
|     13 | WEEK_NUMBER 34 | 2020-08-17   |
|     14 | WEEK_NUMBER 35 | 2020-08-24   |
|     15 | WEEK_NUMBER 36 | 2020-08-31   |< --- period part before new month
|     16 | WEEK_NUMBER 36 | 2020-09-01   |< --- period part before new month (Week_Number has not changed)
|     15 | WEEK_NUMBER 37 | 2020-09-07   | 
|     16 | WEEK_NUMBER 38 | 2020-09-14   |
+--------+----------------+--------------+

您可以使用递归查询计算“周期”数,如下所示。

正如您在问题下的评论中所表达的,我不太明白您在“周”栏中的意思(我怀疑您的定义实际上可能不一致 - 留待您澄清)。

with
  r (plan, period, max_periods, start_date) as (
    select  plan, 1, max_periods, start_date
      from  table_1
    union all
    select  plan, period + 1, max_periods,
            least(start_date + 7, add_months(trunc(start_date, 'mm'), 1))
      from  r
      where period < max_periods
  )
select plan, period, start_date
from   r
;

PLAN     PERIOD START_DATE
---- ---------- ----------
A             1 2020-05-01
A             2 2020-05-08
A             3 2020-05-15
A             4 2020-05-22
A             5 2020-05-29
A             6 2020-06-01
A             7 2020-06-08
A             8 2020-06-15
A             9 2020-06-22
A            10 2020-06-29
A            11 2020-07-01
A            12 2020-07-08

这是基于 TABLE_1 的最大周期,我根本没有使用 TABLE_2。您可以在单独从 TABLE_1 生成此结果集后加入两个表,或根据需要调整方法。