如果间隔小于 5 秒,则将几行合并为一行
Merge several rows into one if they have a gap less than a 5 seconds
我正在尝试找到一个 SQL 查询,它可以让我将间隔小于 5 秒的 table 中的一些行合并为一行。例如,我有一个 table 如下所示:
Name | Time
==============================
John 2021-02-01 13:08:10
John 2021-02-01 13:08:12
John 2021-02-01 17:35:23
John 2021-02-07 11:16:31
Walt 2021-01-14 10:23:48
Joseph 2021-01-23 07:04:33
Walt 2021-01-14 10:23:51
Walt 2021-01-04 09:22:45
所以,我想要这样的结果:
Name | Time
==============================
John 2021-02-01
John 2021-02-01
John 2021-02-07
Walt 2021-01-14
Walt 2021-01-04
Joseph 2021-01-23
对于 John,有两行的间隔小于 5 秒,因此它们将在同一天合并为一行。沃尔特也是如此。
我可以使用 SQL 查询来做到这一点吗?
提前致谢。
您只需要检查下一个日期是否在当前行之后的 5 秒内,如果是,则删除该行。这可以通过 LEAD
分析函数来实现。
with a as (
select 'John' as name, convert(datetime, '2021-02-01 13:08:10', 120) as dt union all
select 'John' as name, convert(datetime, '2021-02-01 13:08:12', 120) as dt union all
select 'John' as name, convert(datetime, '2021-02-01 13:08:15', 120) as dt union all
select 'John' as name, convert(datetime, '2021-02-01 17:35:23', 120) as dt union all
select 'John' as name, convert(datetime, '2021-02-07 11:16:31', 120) as dt union all
select 'Walt' as name, convert(datetime, '2021-01-14 10:23:48', 120) as dt union all
select 'Joseph' as name, convert(datetime, '2021-01-23 07:04:33', 120) as dt union all
select 'Walt' as name, convert(datetime, '2021-01-14 10:23:51', 120) as dt union all
select 'Walt' as name, convert(datetime, '2021-01-04 09:22:45', 120) as dt
)
, gap_size as (
select
name,
dt,
/*Check the difference between current row and the next row per name*/
datediff(s,
dt,
lead(dt) over(partition by name order by dt asc)
) as within_5_secs_with_next
from a
)
select
name,
cast(dt as date) as dt_date
from gap_size
where coalesce(within_5_secs_with_next, 10) >= 5
order by name, dt asc
GO
name | dt_date
:----- | :---------
John | 2021-02-01
John | 2021-02-01
John | 2021-02-07
Joseph | 2021-01-23
Walt | 2021-01-04
Walt | 2021-01-14
db<>fiddle here
我正在尝试找到一个 SQL 查询,它可以让我将间隔小于 5 秒的 table 中的一些行合并为一行。例如,我有一个 table 如下所示:
Name | Time
==============================
John 2021-02-01 13:08:10
John 2021-02-01 13:08:12
John 2021-02-01 17:35:23
John 2021-02-07 11:16:31
Walt 2021-01-14 10:23:48
Joseph 2021-01-23 07:04:33
Walt 2021-01-14 10:23:51
Walt 2021-01-04 09:22:45
所以,我想要这样的结果:
Name | Time
==============================
John 2021-02-01
John 2021-02-01
John 2021-02-07
Walt 2021-01-14
Walt 2021-01-04
Joseph 2021-01-23
对于 John,有两行的间隔小于 5 秒,因此它们将在同一天合并为一行。沃尔特也是如此。
我可以使用 SQL 查询来做到这一点吗?
提前致谢。
您只需要检查下一个日期是否在当前行之后的 5 秒内,如果是,则删除该行。这可以通过 LEAD
分析函数来实现。
with a as ( select 'John' as name, convert(datetime, '2021-02-01 13:08:10', 120) as dt union all select 'John' as name, convert(datetime, '2021-02-01 13:08:12', 120) as dt union all select 'John' as name, convert(datetime, '2021-02-01 13:08:15', 120) as dt union all select 'John' as name, convert(datetime, '2021-02-01 17:35:23', 120) as dt union all select 'John' as name, convert(datetime, '2021-02-07 11:16:31', 120) as dt union all select 'Walt' as name, convert(datetime, '2021-01-14 10:23:48', 120) as dt union all select 'Joseph' as name, convert(datetime, '2021-01-23 07:04:33', 120) as dt union all select 'Walt' as name, convert(datetime, '2021-01-14 10:23:51', 120) as dt union all select 'Walt' as name, convert(datetime, '2021-01-04 09:22:45', 120) as dt ) , gap_size as ( select name, dt, /*Check the difference between current row and the next row per name*/ datediff(s, dt, lead(dt) over(partition by name order by dt asc) ) as within_5_secs_with_next from a ) select name, cast(dt as date) as dt_date from gap_size where coalesce(within_5_secs_with_next, 10) >= 5 order by name, dt asc GO
name | dt_date :----- | :--------- John | 2021-02-01 John | 2021-02-01 John | 2021-02-07 Joseph | 2021-01-23 Walt | 2021-01-04 Walt | 2021-01-14
db<>fiddle here