SQL - 自定义日期组

Posted

技术标签:

【中文标题】SQL - 自定义日期组【英文标题】:SQL - Custom Date Groups 【发布时间】:2021-01-29 10:27:30 【问题描述】:

这个真的把我的头撞到墙上了。

我基本上是在尝试在 SQL 中创建周/月组,因此假设团队成员于 2021 年 5 月 1 日开始他们的合同,那么 05/01/2021 - 12/01/2021 将是第 1 周、第 12 周/01/2021 - 19/01/2021 将是第 2 周,以此类推,直到第 26 周。目的是我可以使用这些周组更轻松地与其他团队成员进行比较。

例如,假设我有这样的简单数据。

Staff_Members Start Date
team Member 1 25/06/2019
team Member 2 30/06/2019

我想用 SQL 把它变成这个。

Staff_Members Start Date Week Group Week Start Week End
Team member 1 25/06/2019 Week 1 25/06/2019 02/07/2019
Team member 1 25/06/2019 Week 2 02/07/2019 09/07/2019
Team member 1 25/06/2019 Week 3 09/07/2019 16/07/2019
Team member 1 25/06/2019 Week 4 16/07/2019 23/07/2019
Team member 1 25/06/2019 Week 5 23/07/2019 30/07/2019
Team member 1 25/06/2019 Week 6 30/07/2019 06/08/2019
Team member 1 25/06/2019 Week 7 06/08/2019 13/08/2019
Team member 1 25/06/2019 Week 8 13/08/2019 20/08/2019
Team member 1 25/06/2019 Week 9 20/08/2019 27/08/2019
Team member 1 25/06/2019 Week 10 27/08/2019 03/09/2019
Team member 1 25/06/2019 Week 11 03/09/2019 10/09/2019
Team member 1 25/06/2019 Week 12 10/09/2019 17/09/2019
Team member 1 25/06/2019 Week 13 17/09/2019 24/09/2019
Team member 1 25/06/2019 Week 14 24/09/2019 01/10/2019
Team member 1 25/06/2019 Week 15 01/10/2019 08/10/2019
Team member 1 25/06/2019 Week 16 08/10/2019 15/10/2019
Team member 1 25/06/2019 Week 17 15/10/2019 22/10/2019
Team member 1 25/06/2019 Week 18 22/10/2019 29/10/2019
Team member 1 25/06/2019 Week 19 29/10/2019 05/11/2019
Team member 1 25/06/2019 Week 20 05/11/2019 12/11/2019
Team member 1 25/06/2019 Week 21 12/11/2019 19/11/2019
Team member 1 25/06/2019 Week 22 19/11/2019 26/11/2019
Team member 1 25/06/2019 Week 23 26/11/2019 03/12/2019
Team member 1 25/06/2019 Week 24 03/12/2019 10/12/2019
Team member 1 25/06/2019 Week 25 10/12/2019 17/12/2019
Team member 1 25/06/2019 Week 26 17/12/2019 24/12/2019
Team member 2 30/06/2019 Week 1 30/06/2019 07/07/2019
Team member 2 30/06/2019 Week 2 07/07/2019 14/07/2019
Team member 2 30/06/2019 Week 3 14/07/2019 21/07/2019
Team member 2 30/06/2019 Week 4 21/07/2019 28/07/2019
Team member 2 30/06/2019 Week 5 28/07/2019 04/08/2019
Team member 2 30/06/2019 Week 6 04/08/2019 11/08/2019
Team member 2 30/06/2019 Week 7 11/08/2019 18/08/2019
Team member 2 30/06/2019 Week 8 18/08/2019 25/08/2019
Team member 2 30/06/2019 Week 9 25/08/2019 01/09/2019
Team member 2 30/06/2019 Week 10 01/09/2019 08/09/2019
Team member 2 30/06/2019 Week 11 08/09/2019 15/09/2019
Team member 2 30/06/2019 Week 12 15/09/2019 22/09/2019
Team member 2 30/06/2019 Week 13 22/09/2019 29/09/2019
Team member 2 30/06/2019 Week 14 29/09/2019 06/10/2019
Team member 2 30/06/2019 Week 15 06/10/2019 13/10/2019
Team member 2 30/06/2019 Week 16 13/10/2019 20/10/2019
Team member 2 30/06/2019 Week 17 20/10/2019 27/10/2019
Team member 2 30/06/2019 Week 18 27/10/2019 03/11/2019
Team member 2 30/06/2019 Week 19 03/11/2019 10/11/2019
Team member 2 30/06/2019 Week 20 10/11/2019 17/11/2019
Team member 2 30/06/2019 Week 21 17/11/2019 24/11/2019
Team member 2 30/06/2019 Week 22 24/11/2019 01/12/2019
Team member 2 30/06/2019 Week 23 01/12/2019 08/12/2019
Team member 2 30/06/2019 Week 24 08/12/2019 15/12/2019
Team member 2 30/06/2019 Week 25 15/12/2019 22/12/2019
Team member 2 30/06/2019 Week 26 22/12/2019 29/12/2019

提前感谢您提供的任何建议。 汤姆

【问题讨论】:

欢迎来到 SO。请参阅Why should I provide an MCRE for what seems to me to be a very simple SQL query 也就是说,数据显示的问题一般最好在应用代码中解决 【参考方案1】:

您可以使用递归 CTE。在 mysql 中,这看起来像:

with recursive cte as (
      select staff_member, start_date, 1 as week_number, start_date as week_start, start_date + interval 7 day as end_date
      from t
      union all
      select staff_member, start_date, week_number + 1, end_date, end_date + interval 7 day
      from cte
      where year(start_date) = year(week_start)
     )
 select *
 from cte
 order by staff_member, week_start;

Here 是一个 dbfiddle。

【讨论】:

谢谢你!直到现在才听说递归 CTE,所以这很有帮助!【参考方案2】:

使用 dateadd(day, 7 , "start-date") 它会将您的开始日期增加 7 天并给出结束日期。

【讨论】:

以上是关于SQL - 自定义日期组的主要内容,如果未能解决你的问题,请参考以下文章

如何为一组自定义分类页面创建可变页面标题

如何从一组自定义字段值中显示 Wordpress 帖子

如何线程安全存档一组自定义对象?

如何将一组自定义结构保存到 plist swift

如何创建一组自定义对象(Swift)?

如何在 Core Data 中存储一组自定义(和重复)对象?