0

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

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

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

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
4

1 回答 1

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
于 2013-02-12T15:28:45.423 回答