所以我的代码完美无瑕,但我需要进行一次升级,但我想不通。基本上。我每年都有一个价格,直到 2052 年(40 年)。我错开这些价格,将每个 ID 的起始年份和 5 个价格连续放置(总共 72 个 ID)。我还有一个从 550 开始的行号,每次循环增加 2。有一个区(我现在知道的)每次通过循环时都需要增加 3。在循环结束时设置变量值时,有没有办法设置 IF 语句或 CASE 语句?所以当我到达“SET @LINE = @LINE + 2”时,我需要一个类似于..
CASE
WHEN DISTRICT = 'ALASKA'
THEN @LINE = @LINE + 3
ELSE @LINE = @LINE + 2
END
这是我现在正在使用的。
DECLARE @IDF INT
DECLARE @FLAG INT
DECLARE @LINE INT
TRUNCATE TABLE #CODE
SET @IDF = 1
WHILE (@IDF < 73)
BEGIN
SET @FLAG = (SELECT min(YEAR) FROM #TABLE1 WHERE ID = @IDF)
SET @LINE = 550
WHILE (@FLAG < 2053)
BEGIN
INSERT INTO #CODE(ID,DISTRICT,year,LINE,t1,t2,t3,t4,t5)
SELECT ID,
DISTRICT,
year,
(@LINE) AS 'LINE',
(SELECT YEARLY_PRICING FROM #TABLE1 b WHERE b.ID = @IDF AND b.YEAR = @FLAG) AS 't1',
(SELECT YEARLY_PRICING FROM #TABLE1 b WHERE b.ID = @IDF AND b.YEAR = @FLAG+1) AS 't2',
(SELECT YEARLY_PRICING FROM #TABLE1 b WHERE b.ID = @IDF AND b.YEAR = @FLAG+2) AS 't3',
(SELECT YEARLY_PRICING FROM #TABLE1 b WHERE b.ID = @IDF AND b.YEAR = @FLAG+3) AS 't4',
(SELECT YEARLY_PRICING FROM #TABLE1 b WHERE b.ID = @IDF AND b.YEAR = @FLAG+4) AS 't5'
FROM #TABLE1 WHERE ID = @IDF AND YEAR = @FLAG
SET @FLAG = @FLAG + 5
SET @LINE = @LINE + 2
END
SET @IDF = @IDF + 1
END