R2 CTE語法錯誤這個問題是基於我剛纔的問題SQL服務器上SQL 2008 SELECT語句
SQL server 2008 R2, select one value of a column for each distinct value of another column
約CTE 2008
WITH my_cte(id_num, rn) AS (
SELECT name,
rn = ROW_NUMBER() OVER (PARTITION BY a.name ORDER BY newid())
FROM my_table as a
)
SELECT id_num FROM my_cte WHERE rn = 1
INSERT INTO #temp_table
SELECT a.address from another_table as a,
id_num from my_cte -- here, I got error!!!
爲什麼我得到錯誤:不正確語法靠近關鍵字'from'。
我需要從my_cte的另一個表和一列中得到一個新表。
例如
address (from another_table) id_num (from my_cte)
city_1 65
city_1 36
city_2 65
city_2 36
city_3 65
city_3 36
什麼樣的加入,我應該用它來得到上面的表,以便每個地址與來自CTE所有ID_NUM相關的?假設id_num只有65和36兩個值。 my_cte沒有地址欄。
任何幫助,將不勝感激。
這究竟是在做什麼? 'SELECT * FROM (從another_table作爲 SELECT a.address,從my_cte ID_NUM - **我得到了錯誤: '從' 關鍵字附近有語法錯誤** )爲t' – JiggsJedi
請參閱我的更新 – user3448011
是的,我看到了..這個'從another_table中選擇a.address作爲a,來自my_cte的id_num是無效的......我在問你打算在這裏做什麼。 – JiggsJedi