使用加权将日期添加到表SQL中

时间:2013-02-12 14:15:42

标签: tsql

我有一个站点表,其中每个站点的评级介于1和4之间,我还有一个容量表,我想使用站点表中的评级添加开始日期,例如

评级为1开始日期的网站应为01/05/2010,结束日期为30/09/2010
评级为2和3的开始日期的网站应为01/04/2010,结束日期为01/11/2010
评级为4开始日期的网站应为01/01/2010,结束日期为2010年12月31日

我已经获得了前两列的帮助,已经看到下面的代码,我现在需要添加开始日期和结束日期。你能帮忙吗?

USE OCCUPANCY
CREATE TABLE Cap1
(PitchType_Skey int, 
 Site_Skey int)

DECLARE @PitchType_Skey INT
DECLARE @Site_Skey INT


SET @PitchType_Skey = 1
SET @Site_Skey = 1


WHILE (@Site_Skey < 127)

BEGIN
    IF @PitchType_Skey = 8

BEGIN
   SET @PitchType_Skey = 1
   SET @Site_Skey = @Site_Skey + 1
END

    IF (@Site_Skey < 127)

BEGIN
     INSERT INTO dbo.Cap1 (PitchType_Skey, Site_Skey)
     SELECT @PitchType_Skey, @Site_Skey
END

  SET @PitchType_Skey = @PitchType_Skey + 1

END

1 个答案:

答案 0 :(得分:0)

在一个命令中并使用ISO日期

INSERT INTO dbo.Cap1 (PitchType_Skey, Site_Skey, StartDate, EndDate)
SELECT
   ROW_NUMBER() OVER (ORDER BY Site_Skey),
   Site_Skey /*from site table column*/,
   CASE rating
      WHEN 1 THEN '20100501'
      WHEN 4 THEN '20100101'
      ELSE '20100401'
   END,
   CASE rating
      WHEN 1 THEN '20100930'
      WHEN 4 THEN '20101231'
      ELSE '20101101'
   END
FROM Site