【问题标题】:SQL, How to create series of dates between given two dates?SQL,如何在给定的两个日期之间创建一系列日期?
【发布时间】:2020-05-23 02:54:40
【问题描述】:

假设你有一个类似 cron 的表达式

2:30 every monday and wednesday 和句号2020.05.23 - 2021.02.02

我想为期间的每个匹配时间创建一行

2:30 2020.05.25 (mon)
2:30 2020.05.27 (wed)

如何批量创建?

(如果重要的话,我在 postgresql 和 django 上)

【问题讨论】:

    标签: sql django postgresql


    【解决方案1】:

    要仅获取您想要的时间戳,您可以使用以下查询

    select d from
        generate_series('2020.05.23 2:30:00'::timestamp, '2021.02.02 2:30:00'::timestamp, '1 day'::interval) d
    where extract(dow from d) in (1,3)
    

    批量插入表格

    Insert into <table_name> (<column_name>)
    select d from
        generate_series('2020.05.23 2:30:00'::timestamp, '2021.02.02 2:30:00'::timestamp, '1 day'::interval) d
    where extract(dow from d) in (1,3)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多