如何填写MySQL中两个日期之间的所有时间段?

时间:2015-08-20 10:03:07

标签: mysql r date user-defined

我有以下情况,我有一些人有开始和结束日期:

ID | start_date | end_date
1    2015-02-15   2015-04-20
2    2015-03-10   2015-06-15
...   ...            ...

现在,我需要派生一个包含个人的表格,并且在开始日期和结束日期之间连续30天的所有期间(从start_date开始)。结果应如下所示:

ID | period | from_date   | to_date
1     1       2015-02-15    2015-03-17
1     2       2015-03-18    2015-04-17
2     1       2015-03-10    2015-04-09
2     2       2015-04-10    2015-05-10
2     3       2015-05-11    2015-06-10

你知道如何在MySQL中以聪明的方式创建这样的表吗?如果MySQL对于这样的数据操作来说太麻烦,R或Excel也适用于我。

3 个答案:

答案 0 :(得分:1)

您可以生成一系列数字,然后将该范围与所有记录交叉连接,将30天的组添加到该行返回的数字。

这样的事情(没有经过测试,请原谅任何错别字): -

SELECT a.id, b.aNum, DATE_ADD(a.start_date, INTERVAL (b.aNum * 30) DAY) AS from_date, DATE_ADD(a.start_date, INTERVAL ((b.aNum + 1) * 30) DAY) AS to_date
FROM sometable a
CROSS JOIN
(
    SELECT tens.aCnt * 10 + units.aCnt AS aNum
    FROM
    (SELECT 1 AS aCnt UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) units
    CROSS JOIN
    (SELECT 1 AS aCnt UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) tens
) b
WHERE  DATE_ADD(a.start_date, INTERVAL (b.aNum * 30) DAY) <= end_date

此版本仅适用于最多100个30天的群组,但可以轻松扩展(但是您应对的群体越多,速度越慢)

答案 1 :(得分:1)

我必须说出棘手的问题。

我尝试使用R 中的data.table。首先,我要确保您的数据中有正确的日期格式

library(data.table)
indx <- grep("date", names(df))
setDT(df)[, (indx) := lapply(.SD, as.Date), .SDcols = indx]

然后,我们将计算每个ID 的30天间隔,同时在开始和结束列中添加累积索引

df[, 
     {
      temp <- seq.Date(start_date, end_date, by = "30 days")
      indx <- seq_along(temp[-(1L:2L)])
      .(
        Period = c(indx, length(temp) - 1L),
        from = c(temp[1L], temp[-c(1L, length(temp))] + indx),
        to = c(temp[2L], temp[-c(1L:2L)] + indx)
       )
      }
, by = ID]

#    ID Period       from         to
# 1:  1      1 2015-02-15 2015-03-17
# 2:  1      2 2015-03-18 2015-04-17
# 3:  2      1 2015-03-10 2015-04-09
# 4:  2      2 2015-04-10 2015-05-10
# 5:  2      3 2015-05-11 2015-06-10

答案 2 :(得分:0)

我稍微调整了Kickstart的代码以解决原始帖子中的所有要求,也许它可以帮助有类似问题的人:

SELECT a.pid, b.aNum+1 as period, DATE_ADD(a.start_date, INTERVAL (b.aNum * 31) DAY) AS from_date, 
DATE_ADD(DATE_ADD(a.start_date, INTERVAL (b.aNum * 31) DAY), INTERVAL 30 DAY) AS to_date
FROM any_table a
CROSS JOIN
(
SELECT hundreds.aCnt*100 + tens.aCnt * 10 + units.aCnt AS aNum
FROM
(SELECT 1 AS aCnt UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) units
CROSS JOIN
(SELECT 1 AS aCnt UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) tens
 CROSS JOIN
(SELECT 1 AS aCnt UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) hundreds
) b
WHERE  DATE_ADD(a.start_date, INTERVAL (b.aNum * 30)+30 DAY) <= end_date

现在,下一个时段在前一个结束后一天开始,个人的最后30天时段在end_date之前结束。