我想創建一個CTE,如下所示調用另一個CTE。但我得到這個錯誤:使用調用另一個CTE的CTE - 近語法錯誤;
Msg 102, Level 15, State 1, Line 54 Incorrect syntax near ';'.
我真的不知道問題是什麼。我檢查了一堆類似於我的&的其他問題,嘗試從這些解決方案,但它不會因爲某些原因工作。
With SEASONALITY_AVG_REVENUE
(
YearKey
,Aggregation_Key
,Yearly_Avg_Revenue
)
as
(
SELECT
ts.YearKey,
agg.Aggregation_Key,
Yearly_Avg_Revenue = AVG(agg.Revenue)
FROM
seasonality_custom.TIME_SERIES_INDEX ts
INNER JOIN seasonality_custom.SEASONALITY_AGG_TIME_SERIES agg
ON ts.TIME_SERIES_INDEX = agg.TIME_SERIES_INDEX
GROUP BY
ts.YearKey,
agg.Aggregation_Key
)
, SEASONALITY_ALL_RECS
(
YearKey
,Aggregation_Key
,Revenue
,Yearly_Avg_Revenue
)
as
(
SELECT
ts.YearKey,
agg.Aggregation_Key,
agg.Revenue,
Yearly_Avg_Revenue = savg.Yearly_Avg_Revenue
FROM
seasonality_custom.TIME_SERIES_INDEX ts
INNER JOIN seasonality_custom.SEASONALITY_AGG_TIME_SERIES agg
ON ts.TIME_SERIES_INDEX = agg.TIME_SERIES_INDEX
INNER JOIN SEASONALITY_AVG_REVENUE savg
ON ts.YearKey = savg.YearKey and agg.Aggregation_Key = savg.Aggregation_Key
);
';'應該去聲明的_end_,而不是開頭。 –
CTE是SQL Server中僅有的兩種情況之一,它要求前面的語句以';'結尾,因此很多SQL開發人員習慣於用';'開始CTE的習慣,用' ;'在SQL開發人員中並不常見。因此,壞/怪異的習慣:) –
堅持這種壞習慣會讓人們認爲關鍵字是'; with'並導致所有類型的奇怪問題,如[this](http://stackoverflow.com/questions/23078215/cte -in-從子句-的-SQL查詢)。即使微軟建議用';'來終止所有查詢(參見例如[here](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/09/03/ladies-and-gentlemen-start-your-semi- colons.aspx)) –