2013-08-06 69 views
-1

我有一個名爲Rateplan表和數據如下:如何更新日期字段的取值範圍

例如:

SELECT Rateplanid 
    , listingid 
    , rentalunitid 
    , validfromdate 
    , validtodate 
FROM Rateplan 
WHERE listingid = 721760 
    AND rentalunitid = 3027217 
ORDER BY 
     listingid 
    , rentalunitid 
    , validfromdate 
    , validtodate 

Rateplanid listingid rentalunitid validfromdate    validtodate 
    3  721760  3027217   2012-08-09 00:00:00.000 2012-10-18 00:00:00.000 
    22563  721760  3027217   2012-10-26 00:00:00.000 2012-11-27 00:00:00.000 
    25412  721760  3027217   2012-10-30 00:00:00.000 2012-10-30 00:00:00.000 
    25421  721760  3027217   2012-10-31 00:00:00.000 2012-11-27 00:00:00.000 
    26945  721760  3027217   2012-11-01 00:00:00.000 2012-11-07 00:00:00.000 
    34807  721760  3027217   2012-11-14 00:00:00.000 2012-11-27 00:00:00.000 
    35947  721760  3027217   2012-11-15 00:00:00.000 2012-11-15 00:00:00.000 
    43793  721760  3027217   2012-11-29 00:00:00.000 2013-01-03 00:00:00.000 
    62665  721760  3027217   2013-01-03 00:00:00.000 2199-12-31 00:00:00.000 

邏輯是Validtodate當前行中需要根據Validfromdate進行更新下一行。 如果nextrow的ValidfromDate爲< =當前行的Validate,那麼它需要更新爲小於Valisfrom下一行的值。 但我們還需要確保更新的Validtodate需要小於或等於當前行的validfromdate。

預期輸出:

Rateplanid listingid rentalunitid validfromdate    validtodate 
    3  721760  3027217   2012-08-09 00:00:00.000 2012-10-18 00:00:00.000 
    22563  721760  3027217   2012-10-26 00:00:00.000 2012-10-29 00:00:00.000 
    25412  721760  3027217   2012-10-30 00:00:00.000 2012-10-30 00:00:00.000 
    25421  721760  3027217   2012-10-31 00:00:00.000 2012-10-31 00:00:00.000 
    26945  721760  3027217   2012-11-01 00:00:00.000 2012-11-07 00:00:00.000 
    34807  721760  3027217   2012-11-14 00:00:00.000 2012-11-14 00:00:00.000 
    35947  721760  3027217   2012-11-15 00:00:00.000 2012-11-15 00:00:00.000 
    43793  721760  3027217   2012-11-29 00:00:00.000 2013-01-02 00:00:00.000 
    62665  721760  3027217   2013-01-03 00:00:00.000 2199-12-31 00:00:00.000 
+1

Rateplan表是否包含主鍵? – Endrju

+0

@zxspectrum是的我已經添加Rateplaid主鍵到我的問題 – peter

回答

1
DECLARE @tmptable as table (listingid int, rentalunitid int, validfromdate datetime, processed int) 
DECLARE @listingid as int 
DECLARE @rentalunitid as int 
DECLARE @validtodate as date 
DECLARE @validfromdate as date 
DECLARE @validtodatenew as date 

set @listingid = 721760 
set @rentalunitid = 3027217 

INSERT INTO @tmptable 
SELECT 
     listingid 
    , rentalunitid 
    , validfromdate 
    , 0 as processed 
FROM Rateplan 
WHERE listingid = @listingid 
    AND rentalunitid = @rentalunitid 

WHILE (SELECT COUNT(*) from @tmptable where processed = 0) > 0 
BEGIN 

SET @validtodate = (SELECT top 1 validtodate from @tmptable where processed = 0) 
SET @validfromdate = (SELECT TOP 1 validfromdate from @tmptable where processed = 0 and validtodate = @validtodate) 

SET @validtodatenew = (SELECT top 1 validfromdate from Rateplan where listingid = @listingid and rentalunitid = @rentalunitid and validfromdate > @validtodate order by validfromdate asc) 

UPDATE rateplan 
SET validtodate = DATEADD(day,-1,@validtodatenew) 
WHERE listingid = @listingid and rentalunitid = @rentalunitid and validtodate = @validtodate and validfromdate = @validfromdate 

UPDATE @tmptable 
SET processed = 1 
WHERE listingid = @listingid and rentalunitid = @rentalunitid and validtodate = @validtodate and validfromdate = @validfromdate 
and processed = 0 

CONTINUE 
END 

如果你對這個表的主鍵,它會這麼容易且不易出錯。在這個例子中,我將使用pkey作爲主鍵。

DECLARE @tmptable as table (pkey int, validfromdate datetime, processed int) 
DECLARE @listingid as int 
DECLARE @rentalunitid as int 
DECLARE @validtodatenew as date 

set @listingid = 721760 
set @rentalunitid = 3027217 

INSERT INTO @tmptable 
SELECT 
    , pkey 
    , validfromdate 
    , 0 as processed 
FROM Rateplan 
WHERE listingid = @listingid 
    AND rentalunitid = @rentalunitid 

WHILE (SELECT COUNT(*) from @tmptable where processed = 0) > 0 
BEGIN 

SET @pkey = (Select top 1 pkey from @tmptable where processed = 0) 

SET @validfromdate = (Select validfromdate from @tmptable where pkey = @pkey) 

SET @validtodatenew = (SELECT top 1 validfromdate from Rateplan where listingid = @listingid and rentalunitid = @rentalunitid and validfromdate > @validfromdate order by validfromdate asc) 

UPDATE rateplan 
SET validtodate = DATEADD(day,-1,@validtodatenew) 
WHERE pkey = @pkey 

UPDATE @tmptable 
SET processed = 1 
WHERE pkey = @pkey and processed = 0 

CONTINUE 
END 

編輯:意識到我是加入了日期,而不是減去日期。

+0

我試過這兩個解決方案,並沒有一個工作 – peter

+0

,我假設你改變了pkey到Rateplanid?你的結果是什麼?請讓我知道這是一個簡單的語法問題還是它改變了錯誤的字段。 – JamesMurray

+0

是的,我把pkey改爲rateplanid。輸出是沒有辦法接近我想要的 – peter