早上好!
我正在尝试将两个查询组合成一个表。 (请参阅下面的代码)
`CREATE TABLE Layer_Loss
(
dYear INT NOT NULL,
EventNum INT NOT NULL,
Loss INT NULL,
Rec_L1 BIGINT NULL,
Rec_L2 BIGINT NULL,
Rec_L3 BIGINT NULL,
Cap_CML_L1 BIGINT NULL,
Cap_CML_L2 BIGINT NULL,
Cap_CML_L3 BIGINT NULL,
)
INSERT INTO Layer_Loss (dYear,EventNum, Loss, Rec_L1, Rec_L2, Rec_L3, Capped_CML_L1, Capped_CML_L2, Capped_CML_L3)
WITH c AS (SELECT Row_number() OVER (ORDER BY dYear) AS rownum,*
FROM Layer_Loss_Capped2)
SELECT *
FROM
(
SELECT dYear, ROW_NUMBER() OVER (Partition by dYear Order by dYear) as Event_Number, Loss
, 'Recovery_L1'=CASE
WHEN Loss<10000000 THEN 0
WHEN Loss<30000000 THEN 20000000-(30000000-Loss)
ELSE 20000000
END
, 'Recovery_L2'=CASE
WHEN Loss<30000000 THEN 0
WHEN Loss<60000000 THEN 30000000-(60000000-Loss)
ELSE 30000000
END
, 'Recovery_L3'=CASE
WHEN Loss<60000000 THEN 0
WHEN Loss<100000000 THEN 40000000-(100000000-Loss)
ELSE 40000000
END
, (SELECT *, 'Capped_CML_L1'=CASE
WHEN d.CML_L1>40000000 THEN 4000000
ELSE d.CML_L1
END
, (SELECT *, 'Capped_CML_L2'=CASE
WHEN d.CML_L2>60000000 THEN 6000000
ELSE d.CML_L1
END
, (SELECT *, 'Capped_CML_L3'=CASE
WHEN d.CML_L1>80000000 THEN 8000000
ELSE d.CML_L1
END
FROM
(
SELECT a.dYear, a.EventNum, a.Loss, a.Rec_L1, SUM(b.Rec_L1) AS CML_L1, SUM(b.Rec_L2) AS CML_L2, SUM(b.Rec_L3) as CML_L3
FROM c a
LEFT JOIN c b ON a.dYear = b.dYear AND b.rownum <= a.rownum
GROUP BY a.dYear, a.rownum, a.EventNum, a.Rec_L1, a.Loss
) AS d
) AS e
FROM ['04_AIR_StdHU_DS_noSS_ByTerr$']
) AS a
DROP TABLE Layer_Loss`
我有它,以便关于'Recovery_L1','Recovery_L2'和'Recovery_L3'的查询是关于表“Layer_Loss”的,我称之为“Rec_L1”,“Rec_L2”和“Rec_L3”。当我尝试添加导致“Capped_CML_L1”,“Capped_CML_L2”和“Capped_CML_L3”的查询时,我收到以下错误:
“Msg 156,Level 15,State 1,Line 14 关键字“WITH”附近的语法不正确。 消息319,第15级,状态1,第14行 关键字'with'附近的语法不正确。如果此语句是公用表表达式,xmlnamespaces子句或更改跟踪上下文子句,则必须以分号结束前一个语句。“
我已尝试移动'WITH'子句,但结果却相同。
此外,这不是我的最终结果。我的下一步是将前一行中的当前行从“Capped_CML_L1”,“Capped_CML_L2”和“Capped_CML_L3”列中减去称为“Inc_Rec_L1”,“Inc_Rec_L2”和“Inc_Rec_L3”的列。我正在考虑使用光标,但我之前从未使用过光标,所以如果您对此有任何建议,那也会很棒!
感谢您的帮助!
编辑:
`CREATE TABLE Layer_Loss
(
dYear INT NOT NULL,
EventNum INT NOT NULL,
Loss INT NULL,
Rec_L1 BIGINT NULL,
Rec_L2 BIGINT NULL,
Rec_L3 BIGINT NULL,
Cap_CML_L1 BIGINT NULL,
Cap_CML_L2 BIGINT NULL,
Cap_CML_L3 BIGINT NULL,
)
;WITH c AS (SELECT Row_number() OVER (ORDER BY dYear) AS rownum,*
FROM Layer_Loss_Capped2)
INSERT INTO Layer_Loss (dYear,EventNum, Loss, Rec_L1, Rec_L2, Rec_L3, Capped_CML_L1, Capped_CML_L2, Capped_CML_L3)
SELECT *
FROM
(
SELECT dYear, ROW_NUMBER() OVER (Partition by dYear Order by dYear) as Event_Number, Loss
, 'Recovery_L1'=CASE
WHEN Loss<10000000 THEN 0
WHEN Loss<30000000 THEN 20000000-(30000000-Loss)
ELSE 20000000
END
, 'Recovery_L2'=CASE
WHEN Loss<30000000 THEN 0
WHEN Loss<60000000 THEN 30000000-(60000000-Loss)
ELSE 30000000
END
, 'Recovery_L3'=CASE
WHEN Loss<60000000 THEN 0
WHEN Loss<100000000 THEN 40000000-(100000000-Loss)
ELSE 40000000
END
, (SELECT *, 'Capped_CML_L1'=CASE
WHEN d.CML_L1>40000000 THEN 4000000
ELSE d.CML_L1
END
, (SELECT *, 'Capped_CML_L2'=CASE
WHEN d.CML_L2>60000000 THEN 6000000
ELSE d.CML_L1
END
, (SELECT *, 'Capped_CML_L3'=CASE
WHEN d.CML_L1>80000000 THEN 8000000
ELSE d.CML_L1
END
FROM
(
SELECT a.dYear, a.EventNum, a.Loss, a.Rec_L1, SUM(b.Rec_L1) AS CML_L1, SUM(b.Rec_L2) AS CML_L2, SUM(b.Rec_L3) as CML_L3
FROM c a
LEFT JOIN c b ON a.dYear = b.dYear AND b.rownum <= a.rownum
GROUP BY a.dYear, a.rownum, a.EventNum, a.Rec_L1, a.Loss
) AS d
FROM ['04_AIR_StdHU_DS_noSS_ByTerr$']
) AS e
) AS f
) AS g
) AS a
DROP TABLE Layer_Loss`
当我输入上面编辑的代码时,我收到错误: 消息156,第15级,状态1,第58行 关键字“FROM”附近的语法不正确。
我希望稍后可以在另一个查询或表或游标中引用Capped_CML_L1,Capped_CML_L2和Capped_CML_L3。我希望它只是在'e'之下,但我不确定如何使用括号
答案 0 :(得分:0)
;
将 WITH
与任何前面的命令分开。
当CTE在作为批处理一部分的语句中使用时,其前面的语句必须后跟分号。
此外,它必须是整个陈述的第一部分,无论是普通SELECT
还是INSERT
。尝试:
/* CREATE TABLE */
;WITH c AS (SELECT Row_number() OVER (ORDER BY dYear) AS rownum,*
FROM Layer_Loss_Capped2)
INSERT INTO Layer_Loss (dYear,EventNum, Loss, Rec_L1, Rec_L2, Rec_L3,
Capped_CML_L1, Capped_CML_L2, Capped_CML_L3)
SELECT *
FROM
(
SELECT dYear, ROW_NUMBER() OVER ...
(还在WITH
之前移动INSERT
,意识到正在尝试的内容)
另见Transact SQL Syntax conventions:
;
Transact-SQL语句终止符。虽然此版本的SQL Server中的大多数语句不需要分号,但在将来的版本中将需要使用分号。
其中一个主要原因是关键字WITH
的预先使用会修改SELECT
语句。通过坚持;
,它使解析变得更容易。