如果 SQL Server 中的日期序列存在间隙,如何查找 MinDate / MaxDate?

发布于 2025-01-16 13:25:19 字数 896 浏览 0 评论 0原文

我的数据集如下所示,我需要通过按名称和日期列对它们进行分组来生成开始日期(分钟)和结束日期(分钟)。当类型更改时,按逻辑分组应该中断并采用最大日期直到那里。

名称类型日期
Axx1/1/2018
Axx1/2/2018
Ayy1/3/2018
Axx1/4/2018
Axx1/5/2018
Axx1/6/2018

输出如下:

名称类型开始日期结束日期
Axx1/1/20181/2/2018
Ayy2018年1月3日2018年1月3日
Axx2018年1月4日2018年1月6日

My dataset looks like this and I need to generate the StartDate (Min), EndDate(Min) by grouping them by Name and Date columns. When Type changes, the group by logic should break and take Max date till there.

NameTypeDate
Axx1/1/2018
Axx1/2/2018
Ayy1/3/2018
Axx1/4/2018
Axx1/5/2018
Axx1/6/2018

The output would be like:

NameTypeStartDateEndDate
Axx1/1/20181/2/2018
Ayy1/3/20181/3/2018
Axx1/4/20181/6/2018

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝海 2025-01-23 13:25:19

下面的方法有点笨拙,但仍能获取所需的输出。存储桶根据日期(日)差异进行分区。

declare @tbl table(name varchar(5),type varchar(5),[date] date)

insert into @tbl
values('A','xx','1/1/2018')
,('A','xx','1/2/2018')
,('A','yy','1/3/2018')
,('A','xx','1/4/2018')
,('A','xx','1/5/2018')
,('A','xx','1/6/2018')

select distinct name,type
,min(date)over(partition by name,type,diffmodified order by diffmodified) as [StartDate]
,max(date)over(partition by name,type,diffmodified order by diffmodified) as [EndDate]
from(
select *
,case when max(diff)over(partition by name,type order by [date]) > 1
then max(diff)over(partition by name,type order by [date]) else diff end as [diffmodified]
from(
select *, 
isnull(DATEDIFF(day, lag([date],1)
over(partition by name,type order by [date]), [date] ),1)[diff]
from
@tbl)
t)t

Below approach would be bit clumsy yet fetches the desired output. The buckets are partitioned based on the date (day) difference.

declare @tbl table(name varchar(5),type varchar(5),[date] date)

insert into @tbl
values('A','xx','1/1/2018')
,('A','xx','1/2/2018')
,('A','yy','1/3/2018')
,('A','xx','1/4/2018')
,('A','xx','1/5/2018')
,('A','xx','1/6/2018')

select distinct name,type
,min(date)over(partition by name,type,diffmodified order by diffmodified) as [StartDate]
,max(date)over(partition by name,type,diffmodified order by diffmodified) as [EndDate]
from(
select *
,case when max(diff)over(partition by name,type order by [date]) > 1
then max(diff)over(partition by name,type order by [date]) else diff end as [diffmodified]
from(
select *, 
isnull(DATEDIFF(day, lag([date],1)
over(partition by name,type order by [date]), [date] ),1)[diff]
from
@tbl)
t)t
御守 2025-01-23 13:25:19

本例中的挑战是通过列 NameType 识别所有目标组,同时考虑到差距。作为一种可能的解决方案,您可以根据按 Date 排序的 Row_Number 与按 Date 排序的 Row_Number 之间的差异,使用额外的分组表达式按名称、类型分区

With A As (
Select Name, [Type], [Date], 
        Row_Number() Over (Order by [Date]) As Num, 
        Row_Number() Over (Partition by Name, [Type] Order by [Date]) As Num_1
From Tbl)
Select Name, [Type], 
       Convert(VarChar(10), Min([Date]), 103) As StartDate,
       Convert(VarChar(10), Max([Date]), 103) As EndDate
From A
Group by Name, [Type], Num - Num_1
Order by StartDate

dbfiddle

名称类型StartDateEndDate
Axx01/01/201801/02/2018
一个yy01/03/201801/03/2018
Axx01/04/201801/06/2018

The challenge in this case is to identify all target groups by the columns Name and Type taking into account the gaps. As a possible solution, you can use an additional grouping expression based on the difference between Row_Number ordered by Date and Row_Number ordered by Date with Partion by Name, Type.

With A As (
Select Name, [Type], [Date], 
        Row_Number() Over (Order by [Date]) As Num, 
        Row_Number() Over (Partition by Name, [Type] Order by [Date]) As Num_1
From Tbl)
Select Name, [Type], 
       Convert(VarChar(10), Min([Date]), 103) As StartDate,
       Convert(VarChar(10), Max([Date]), 103) As EndDate
From A
Group by Name, [Type], Num - Num_1
Order by StartDate

dbfiddle

NameTypeStartDateEndDate
Axx01/01/201801/02/2018
Ayy01/03/201801/03/2018
Axx01/04/201801/06/2018
悍妇囚夫 2025-01-23 13:25:19

希望这能让你澄清。

select Name,Type,min(date) as StartDate,max(date) as EndDate
from Table_Name
group by Type,Name

Hope this clarify you.

select Name,Type,min(date) as StartDate,max(date) as EndDate
from Table_Name
group by Type,Name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文