每次增加1天的日期

时间:2014-02-27 14:18:09

标签: sql sql-server datetime

在SQL中,我有一个输出表,如下所示:

MemNo | PipNomDt | PIPStrtDt 1739 | 2007-03-31 00:00:00.000 | 2009-02-01 00:00:00.000 1739 | NULL | 2007-04-01 00:00:00.000 1739 | NULL | 2008-04-01 00:00:00.000 1739 | NULL | 2009-04-01 00:00:00.000 1739 | NULL | 2010-04-01 00:00:00.000 1739 | NULL | 2011-04-01 00:00:00.000 1739 | NULL | 2012-04-01 00:00:00.000 1739 | NULL | 2013-04-01 00:00:00.000

我需要更新此表,以便在填充Date1的第一行时,下面的所有行都会以1天的增量填充,因此它将显示为以下内容。

MemNo | PipNomDt | PIPStrtDt 1739 | 2007-03-31 00:00:00.000 | 2009-02-01 00:00:00.000 1739 | 2007-04-01 00:00:00.000 | 2007-04-01 00:00:00.000 1739 | 2007-04-02 00:00:00.000 | 2008-04-01 00:00:00.000 1739 | 2007-04-03 00:00:00.000 | 2009-04-01 00:00:00.000 1739 | 2007-04-04 00:00:00.000 | 2010-04-01 00:00:00.000 1739 | 2007-04-05 00:00:00.000 | 2011-04-01 00:00:00.000 1739 | 2007-04-06 00:00:00.000 | 2012-04-01 00:00:00.000 1739 | 2007-04-07 00:00:00.000 | 2013-04-01 00:00:00.000

我确实尝试了以下代码,但我不确定如何实际执行此操作

            UPDATE  pip
            SET     PIPNomDt = DATEADD(DAY, rn - 1,
                                       ( SELECT TOP 1
                                                n.PIPNomDt
                                         FROM   dbo.wtPIPNomDt_List n ,
                                                dbo.wtPIPStrtDt_List s
                                         WHERE  n.RowID = s.RowID
                                       ))
            FROM    dbo.htPensionInputPeriod pip
                    JOIN ( SELECT   ROW_NUMBER() OVER ( ORDER BY MemNo ) rn ,
                                    MemNo
                           FROM     dbo.htPensionInputPeriod
                         ) t2_numbered ON t2_numbered.MemNo = @MemNo

2 个答案:

答案 0 :(得分:1)

你在找这个吗?

WITH cte AS
(SELECT *, 
            Row_number() 
              OVER( 
                partition BY memno 
                ORDER BY CASE WHEN pipnomdt IS NULL THEN 1 ELSE 0 END, 
              pipstrtdt) 
            AS 
               RN 
     FROM   table1
)
,cte_next AS 
(SELECT * 
     FROM   cte 
     WHERE  rn = 1 
     UNION ALL 
     SELECT C1.memno, 
            dateadd(day,1,c1.pipnomdt), 
            c2.pipstrtdt, 
            c2.rn 
     FROM   cte_next c1 
            JOIN cte c2 
              ON c1.memno = c2.memno 
                 AND c1.rn = c2.rn - 1
) 
SELECT * 
FROM   cte_next 

SQL Fiddle Here

答案 1 :(得分:0)

我的以下测试能够产生类似于您正在寻找的结果:我不确定我是否正确理解了MemNo和RowID之间的关系,但它应该给你一个开始。我假设你只想用RowID的顺序用匹配的MemNo更新值 - 如果需要你可以改变它。

CREATE TABLE pip
  (
RowID INT IDENTITY(1,1) NOT NULL,
MemNo INT NOT NULL,
Date1 DATETIME NULL
  )

INSERT INTO pip (MemNo, Date1) VALUES (1, '8/3/2013')
INSERT INTO pip (MemNo) VALUES (1739)
INSERT INTO pip (MemNo) VALUES (1739)
INSERT INTO pip (MemNo) VALUES (1739)
INSERT INTO pip (MemNo) VALUES (1739)
INSERT INTO pip (MemNo) VALUES (1739)
INSERT INTO pip (MemNo) VALUES (1739)
INSERT INTO pip (MemNo) VALUES (1739)
INSERT INTO pip (MemNo) VALUES (1739)
INSERT INTO pip (MemNo) VALUES (1739)

GO

CREATE TRIGGER T_pip ON pip
    FOR UPDATE 
AS 
  BEGIN 
    UPDATE pip2
    SET pip2.date1 = DATEADD(DAY, s.RowNum, inserted.Date1)
    FROM 
        inserted 
         INNER JOIN 
          (
            SELECT
                RowID,
                MemNo,
                ROW_NUMBER() OVER (PARTITION BY MemNo ORDER BY RowID )     RowNum
            FROM pip
          ) s ON 
            inserted.RowID < s.RowID AND
            inserted.MemNo = s.MemNo
         INNER JOIN 
        pip pip2 ON 
            s.RowID = pip2.RowID


  END

GO

SELECT * FROM pip

UPDATE pip
SET Date1 = '3/31/2007' 
WHERE pip.RowID = 2

SELECT * FROM pip


-- DROP TABLE pip