0

早上好!

我正在尝试组合两个查询来制作一个表格。(请看下面的代码)

`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'附近的语法不正确。Msg 319, Level 15, State 1, Line 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`

当我输入上面编辑的代码时,我得到错误:Msg 156, Level 15, State 1, Line 58 关键字'FROM'附近的语法不正确。

我希望稍后能够在另一个查询或表或游标中引用 Capped_CML_L1、Capped_CML_L2 和 Capped_CML_L3。我希望它在'e'下,但我不确定括号如何

4

1 回答 1

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 语法约定

;Transact-SQL 语句终止符。尽管在此版本的 SQL Server 中大多数语句不需要分号,但在未来的版本中将需要它。

造成这种情况的主要原因之一是WITH修改SELECT语句的关键字已经存在。通过坚持;,它使解析更容易。

于 2013-06-20T14:30:58.853 回答