我有一个名为 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 更新。如果下一行的 ValidfromDate <= 当前行的 Validtodate ,则需要将其更新为比下一行的 ValisfromDate 值少一天。但我们还需要确保更新的 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