0

甲骨文数据库:SQL_FIDDLE

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

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

表_2 列出了表_1 中的期间和相应的计划。

选择

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   |
+--------+----------------+--------------+
4

1 回答 1

1

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

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

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 生成此结果集后加入这两个表,或者根据需要调整方法。

于 2020-12-16T23:36:45.537 回答