简体   繁体   English

MySQL 或 MariaDB 将单行中的日期范围扩展到多列行中

[英]MySQL or MariaDB spread Date range in single row into multiple series of rows

Example tables are below,示例表如下,

CREATE TABLE TIMEOFF_INFO (
  TIMEOFF_ID INT PRIMARY KEY,
  TIMEOFF_BEGIN DATE NOT NULL,
  TIMEOFF_END DATE NOT NULL
)

and example rows below,和下面的示例行,

+------------+---------------+-------------+
| timeoff_id | timeoff_begin | timeoff_end |
+------------+---------------+-------------+
|      1     | 2021-10-01    | 2021-10-02   |
+------------+---------------+-------------+
|      2     | 2021-11-15    | 2021-11-15  |
+------------+---------------+-------------+
|      3     | 2021-12-18    | 2021-12-20  |
+------------+---------------+-------------+

What I want to get is convert table above to below so that I can join using each date in range.我想要得到的是将上面的表格转换为下面的表格,以便我可以使用范围内的每个日期加入。

2021-10-01 (id: 1's begin date)
2021-10-02 (id: 1's end date)
2021-11-15 (id: 2's begin and end date)
2021-12-18 (id: 3's begin date)
2021-12-19
2021-12-20 (id: 3's end date)

Is there any way to extend date range in single row to series of Date row?有没有办法将单行中的日期范围扩展到一系列日期行?

You could use a union all on the same table您可以在同一张桌子上使用联合

    select id, timeoff_begin timeoff, concat(id, ' begin date') msg
    from TIMEOFF_INFO
    union all 
    select id, timeoff_end, concat(id, ' end date') 
    from TIMEOFF_INFO
    order by id, timeoff
WITH RECURSIVE
daterange AS ( SELECT MIN(TIMEOFF_BEGIN) dStart, MAX(TIMEOFF_END) dEnd
               FROM TIMEOFF_INFO ),
calendar AS ( SELECT dStart `date` 
              FROM daterange
              UNION ALL
              SELECT `date` + INTERVAL 1 DAY
              FROM calendar
              WHERE `date` < ( SELECT dEnd FROM daterange ) )
SELECT calendar.`date`
FROM calendar
WHERE EXISTS ( SELECT NULL
               FROM TIMEOFF_INFO 
               WHERE calendar.`date` BETWEEN TIMEOFF_INFO.TIMEOFF_BEGIN AND TIMEOFF_INFO.TIMEOFF_END )

MariaDB 10.2+ or MySQL 8+ needed.需要 MariaDB 10.2+ 或 MySQL 8+。

Much the same as @akina but less sophisticated.与@akina 大致相同,但不那么复杂。 Here a cte is used simply to identify those where the date difference is > 1 then join to a dates table and unioned.此处 cte 仅用于识别日期差大于 1 的那些,然后加入日期表并合并。 the begin, intermediate and end dates are identified by a flag which is used to order by.开始日期、中间日期和结束日期由用于排序的标志标识。 This could be done without a cte but a cte does help clarify(i think), Note I have amended the sample data slightly这可以在没有 cte 的情况下完成,但 cte 确实有助于澄清(我认为),注意我已经稍微修改了示例数据

drop table if exists t;
create table t
(timeoff_id int, timeoff_begin date, timeoff_end date);
insert into t values
(      1     , '2020-10-01'    , '2020-10-05'),  
(      2     , '2020-11-15'    , '2020-11-15'),  
(      3     , '2020-12-18'    , '2020-12-21');  

with cte as
(select 2 as beg,timeoff_id , timeoff_begin, timeoff_end ,datediff(timeoff_end, timeoff_begin) diff 
from t
where datediff(timeoff_end, timeoff_begin) > 1)
select cte.beg,cte.timeoff_id,dates.dte
from cte 
join dates where dates.dte > cte.timeoff_begin and dates.dte <= date_add(cte.timeoff_begin, interval cte.diff -1 day) 
union all
(select 1 as beg,timeoff_id , timeoff_begin dt from t 
union all
select 3 ,timeoff_id , timeoff_end from t 
)
order by timeoff_id, beg,dte;

+-----+------------+------------+
| beg | timeoff_id | dte        |
+-----+------------+------------+
|   1 |          1 | 2020-10-01 |
|   2 |          1 | 2020-10-02 |
|   2 |          1 | 2020-10-03 |
|   2 |          1 | 2020-10-04 |
|   3 |          1 | 2020-10-05 |
|   1 |          2 | 2020-11-15 |
|   3 |          2 | 2020-11-15 |
|   1 |          3 | 2020-12-18 |
|   2 |          3 | 2020-12-19 |
|   2 |          3 | 2020-12-20 |
|   3 |          3 | 2020-12-21 |
+-----+------------+------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM