【问题标题】:Training schedule till end of year培训计划至年底
【发布时间】:2021-03-21 09:01:58
【问题描述】:

我有一个训练时间表,每周 3 次,例如 - 周一、周三、周五。我需要为我的日程表生成记录,日期截止到我接受培训的当年年底。

时间表是:

CREATE TABLE trainingSchedule (
id NUMBER,
training_date DATE
);

如果培训日期已经存在 - 不要插入记录。

【问题讨论】:

  • Edit 问题并展示您已经尝试过的内容。解释失败的原因/位置。具体(错误消息、意外结果等)。
  • 不知从何说起?我的意思是,我无法开始使用任何函数或过程……不知道。请问您有什么建议吗?
  • 没有问题;有什么问题?
  • 使用电子表格可能更容易实现。

标签: oracle date plsql


【解决方案1】:

这是一种选择。在代码中读取 cmets。

SQL> CREATE TABLE trainingSchedule
  2    (id            NUMBER,
  3     training_date DATE
  4  );

Table created.

SQL> create sequence seq_tra;

Sequence created.

SQL> -- initial insert (just to show that MERGE will skip it
SQL> insert into trainingschedule values (seq_tra.nextval, date '2021-03-22');

1 row created.

MERGE 将跳过已经插入的行。我知道您只想插入今天日期之后的日期;如果不是这样,只需删除最后一个条件。

SQL> merge into trainingschedule t
  2  using (-- this is a calendar for current year
  3         select trunc(sysdate, 'yyyy') + level - 1 datum
  4         from dual
  5         connect by level <= add_months(trunc(sysdate, 'yyyy'), 12) - trunc(sysdate, 'yyyy')
  6        ) c
  7    on (c.datum = t.training_date)
  8    when not matched then insert (id, training_date) values (seq_tra.nextval, c.datum)
  9    -- insert only Mondays, Wednesdays and Fridays
 10    where to_char(c.datum, 'dy', 'nls_date_language = english') in ('mon', 'wed', 'fri')
 11    -- insert only dates that follow today's date ("till the end of the current year")
 12      and datum >= trunc(sysdate);

122 rows merged.

SQL>

里面有什么?

SQL> select id,
  2    to_char(training_date, 'dd.mm.yyyy, dy', 'nls_date_language = english') tr_date
  3  from trainingschedule
  4  order by training_date;

        ID TR_DATE
---------- ------------------------
         1 22.03.2021, mon           --> see? No duplicates
       311 24.03.2021, wed
       309 26.03.2021, fri
       207 29.03.2021, mon
       354 31.03.2021, wed
       321 02.04.2021, fri
<skip>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 2021-11-28
    • 2012-06-15
    • 1970-01-01
    • 2017-12-31
    • 2011-10-22
    相关资源
    最近更新 更多