SQL计算日期范围内的连续天数

SQL计算日期范围内的连续天数,sql,sql-server,datetime,count,gaps-and-islands,Sql,Sql Server,Datetime,Count,Gaps And Islands,我试图计算一个人可能拥有的连续天数,唯一的问题是我有日期范围,而不是一个直接的日期列表。以下是我所指范围的示例: Name Start_Date End_Date Johnny 2020-01-02 2020-01-04 Johnny 2020-01-05 2020-01-05 Johnny 2020-01-06 2020-01-10 Jenny 2020-02-07 2020-02-07 Jenny 2020-02-10 2020-02-11 Jenny 2

我试图计算一个人可能拥有的连续天数,唯一的问题是我有日期范围,而不是一个直接的日期列表。以下是我所指范围的示例:

Name    Start_Date  End_Date
Johnny  2020-01-02  2020-01-04
Johnny  2020-01-05  2020-01-05
Johnny  2020-01-06  2020-01-10
Jenny   2020-02-07  2020-02-07
Jenny   2020-02-10  2020-02-11
Jenny   2020-02-12  2020-02-12
开始日期和结束日期分为两列

我试图实现的结果是:

Johnny has 9 consecutive days
Jenny  has 3 consecutive days
我遇到过解决方案的例子,但我找不到一个适合我的问题,即日期范围

迄今为止使用的代码示例:

具有 datesdate,员工编号为 选择“开始日期”作为日期、名称 从myTABLE 其中name=Jenny , 组作为 选择 按日期排列的订单行号,如rn、名称、, dateaddday,-按日期排列的订单行号,日期为grp, 日期 从日期开始 选择 名称 将*计算为连续数, MINdate作为MINdate, MAXdate作为MAXdate 来自团体 按grp、名称分组
这是一个缺口和孤岛问题。一种选择是使用滞后和窗口和来构建相邻记录的组。然后,您可以按组进行聚合并计算连续天数,最后按名称筛选最大连胜:

select name, max(consecutive_days) consecutive_days
from (
    select name, datediff(day, min(start_date), max(end_date)) + 1 consecutive_days
    from (
        select t.*, 
            sum(case when start_date = dateadd(day, 1, lag_end_date) then 0 else 1 end) over(partition by name order by start_date) grp
        from (
            select t.*, 
                lag(end_date) over(partition by name order by start_date) lag_end_date
            from mytable t
        ) t
    ) t
    group by name, grp
) t
group by name
:


这样的办法应该行得通。将子选择替换为表

select name, DATEDIFF(dd, MIN(Start_date),MAX(end_date)) +1 from 

(
select a.name,a.start_date,b.end_date from 
(SELECT 'Johnny' name , '2020-01-02' start_date,  '2020-01-04' end_date
UNION SELECT 'Johnny' , '2020-01-05',  '2020-01-05'
UNION SELECT 'Johnny' , '2020-01-06',  '2020-01-10'
UNION SELECT 'Jenny'  , '2020-02-07',  '2020-02-07'
UNION SELECT 'Jenny'  , '2020-02-10',  '2020-02-11'
UNION SELECT 'Jenny'  , '2020-02-12',  '2020-02-12') a
LEFT join (SELECT 'Johnny' name , '2020-01-02' start_date,  '2020-01-04' end_date
UNION SELECT 'Johnny' , '2020-01-05',  '2020-01-05'
UNION SELECT 'Johnny' , '2020-01-06',  '2020-01-10'
UNION SELECT 'Jenny'  , '2020-02-07',  '2020-02-07'
UNION SELECT 'Jenny'  , '2020-02-10',  '2020-02-11'
UNION SELECT 'Jenny'  , '2020-02-12',  '2020-02-12') b on DATEADD(dd,1,a.end_date ) = b.start_date and a.name = b.name
)q

group by name

您正在使用哪个数据库?MySQL或sql server,请指定一个。您好,我使用的是sql server。您能给我们一个您迄今为止尝试过的示例吗?@holder我已经用我一直使用的代码示例编辑了我的问题
select name, DATEDIFF(dd, MIN(Start_date),MAX(end_date)) +1 from 

(
select a.name,a.start_date,b.end_date from 
(SELECT 'Johnny' name , '2020-01-02' start_date,  '2020-01-04' end_date
UNION SELECT 'Johnny' , '2020-01-05',  '2020-01-05'
UNION SELECT 'Johnny' , '2020-01-06',  '2020-01-10'
UNION SELECT 'Jenny'  , '2020-02-07',  '2020-02-07'
UNION SELECT 'Jenny'  , '2020-02-10',  '2020-02-11'
UNION SELECT 'Jenny'  , '2020-02-12',  '2020-02-12') a
LEFT join (SELECT 'Johnny' name , '2020-01-02' start_date,  '2020-01-04' end_date
UNION SELECT 'Johnny' , '2020-01-05',  '2020-01-05'
UNION SELECT 'Johnny' , '2020-01-06',  '2020-01-10'
UNION SELECT 'Jenny'  , '2020-02-07',  '2020-02-07'
UNION SELECT 'Jenny'  , '2020-02-10',  '2020-02-11'
UNION SELECT 'Jenny'  , '2020-02-12',  '2020-02-12') b on DATEADD(dd,1,a.end_date ) = b.start_date and a.name = b.name
)q

group by name