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

时间:2019-05-21 07:21:32

标签: sql sql-server sql-server-2008

一个表看起来像这样:

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..   

我需要一个查询,该查询将显示每种状态花费的天数。如果我继承的表有结束日期,那将很容易。然后,我将计算datediff并为列status的值进行透视。

也许我应该使用ssis创建一个新表,在其中我将添加一个EndDt列,该列将是最新添加的Status的StartDt。 但是有什么方法可以在不创建另一个表的情况下做到这一点?

2 个答案:

答案 0 :(得分:1)

SQL Server 2008

  

这不是很漂亮,而且我还没有针对所有用例进行过测试。希望您可以使用它或寻找灵感。我确定有更好的方法:)

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

  

这可能会更漂亮:)

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)日期的规则

enter image description here

答案 1 :(得分:0)

也许LEAD功能对您的问题很有用。

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