计算数据集不同行中日期之间的日期差异

Calculate date difference between dates in different rows of a dataset

一个 table 看起来像这样:

CREATE TABLE [dbo].[HistDT](
    [ID] [bigint] NULL,
    [StartDtSK] [varchar](8) NULL,
    [StartDt] [datetime] NULL,
    [status] [nvarchar](30) NULL,
) ON [PRIMARY]

示例数据集:

ID | StartDtSK | StartDt              | Status |
1     20190520   20-05-2019 12:00:13      10
1     20190520   20-05-2019 10:00:00       5
1     20190414   14-04-2019 13:23:00       2
2     20190312   12-03-2019 10:03:00      10
2     20190308   08-03-2019 18:03:00       1
etc..   

我需要一个查询来显示在每种状态下花费的天数。如果我继承的 table 有一个结束日期,那会很容易。然后我会计算 datediff 和列 status 值的数据透视表。

也许我应该使用 ssis 创建一个新的 table,我将在其中添加一个 EndDt 列,该列将成为最新添加的 Status 的 StartDt。 但是有没有办法在不创建另一个 table 的情况下做到这一点?

也许 LEAD 函数对您的问题有用。

像这样

IsNull(DateAdd(SECOND,-1,Cast(LEAD ([StartDt],1) OVER (PARTITION BY [status] ORDER BY [StartDt]) AS DATETIME)),getdate()) AS EndDate

SQL 服务器 2008

This is not very pretty, and I haven't tested it for all use cases. I hope you can use it or find inspiration. I'm sure there is a better way :)

declare @table2 table (
    [ID] [bigint] NULL,
    [StartDtSK] [varchar](8) NULL,
    [StartDt] [datetime] NULL,
    [status] [nvarchar](30) NULL
) 

insert into @table2

values
(1 ,   '20190520','2019-05-20 12:00:13','10'),


(1 ,   '20190520','2019-05-20 10:00:00','5'),

(1 ,   '20190414','2019-04-14 13:23:00','2'),
(2,     '20190312',   '2019-03-12 10:03:00',      '10'),
(2 ,    '20190308',   '2019-03-08 18:03:00',       '1')

select *,DATEDIFF(dd,startdt,enddate) as TotalDAys from (
select x.ID,StartDtSK,Startdt,[Status],Enddate from (
select *,ROW_NUMBER() over(partition by id order by startdt) as rn from @table2
) x
cross apply ( select * from (select id,StartDt as Enddate,ROW_NUMBER() over(partition by id order by startdt) as rn2  from @table2 b
)f where (rn +1 = f.rn2 ) and x.id = f.id ) d

union all
select ID,StartDtSK,startdt,[Status],'9999-12-31' as Enddate from (
select *,ROW_NUMBER() over(partition by id order by startdt desc) as rn from @table2
)X where rn=1
)y 
order by id,startdt

SQL Server 2008 没有交叉应用

This might be a bit more pretty :)

select *,DATEDIFF(dd,startdt,enddate) as TotalDAys from (
select x.ID,StartDtSK,Startdt,[Status],case when Enddate is null then '9999-12-31' else Enddate end as Enddate from (
select *,ROW_NUMBER() over(partition by id order by startdt) as rn from @table2
) x
left join ( 
select * from (select id,StartDt as Enddate,ROW_NUMBER() over(partition by id order by startdt) as rn2  from @table2 b
)f  ) d on  (rn +1 = d.rn2 ) and x.id = d.id

)y 

SQL Server 2012及以上版本:

这是你想要的吗?

declare @table2 table (
    [ID] [bigint] NULL,
    [StartDtSK] [varchar](8) NULL,
    [StartDt] [datetime] NULL,
    [status] [nvarchar](30) NULL
) 

insert into @table2

values
(1 ,   '20190520','2019-05-20 12:00:13','10'),


(1 ,   '20190520','2019-05-20 10:00:00','5'),

(1 ,   '20190414','2019-04-14 13:23:00','2')

select *,Datediff(dd,Startdt,Enddate) as TotalDays from (
select *,LAG(StartDt,1,'9999-12-31') over(partition by ID order by StartDT desc) as EndDate from @table2
)x

插入处理当前状态 (9999-12-31) 日期的规则