-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 が <= 現在の行の Validtodate である場合、次の行の値の ValisfromDate よりも 1 日短いものとして更新する必要があります。ただし、更新された 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
4

1 に答える 1

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

編集:日付を減算するのではなく、日付を追加していることに気付きました。

于 2013-08-07T16:34:38.623 に答える