嗯,知道了! 請參閱下面的代碼:
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[MyDates]') AND type in (N'U'))
DROP TABLE [dbo].[MyDates]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[MyDates](
[id] [int] IDENTITY(1,1) NOT NULL,
[dt] [datetime] NULL
) ON [PRIMARY]
GO
insert into MyDates
select convert(datetime,convert(date,getdate()))
go
insert into MyDates
select null
go
insert into MyDates
select null
go
insert into MyDates
select convert(datetime,convert(date,getdate()+3))
go
insert into MyDates
select null
go
insert into MyDates
select convert(datetime,convert(date,getdate()+8))
go
--Before
select *
from Mydates
--After
;with Finish as
(
select isnull(b.id , a.id) id , a.dt ,
case
when a.dt is null
then (select max(m.dt) from MyDates m where m.id <= isnull(b.id , a.id))
else a.dt
end FullDt
from MyDates a
left join MyDates b
on a.dt <= b.dt
)
select Id,
max(coalesce (dt,FullDt)) FullDt
from Finish
group by id
我不想使用硬編碼更新。請記住,我想用以前的日期替換NULLS – hkravitz 2014-09-05 06:59:35
col「dt2」的數據類型是什麼? – Justin 2014-09-05 07:02:59
日期時間。我需要編寫一個查詢來返回我在文章中提到的輸出 – hkravitz 2014-09-05 07:06:46