【发布时间】:2014-06-27 15:46:35
【问题描述】:
这个问题是基于我之前的问题
SQL server 2008 R2, select one value of a column for each distinct value of another column
关于 SQL Server 2008 上的 CTE。
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”附近的语法不正确。
我需要获取一个新表,其中一列来自 another_table,一列来自 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 ( SELECT a.address from another_table as a, id_num from my_cte -- **I got error: Incorrect syntax near the keyword 'from'.** ) as t -
是的,我明白了..这个
SELECT a.address from another_table as a, id_num from my_cte无效...我在问你打算在这里做什么。 -
您是否已经创建了临时表,或者您正在尝试选择进入?
标签: sql sql-server sql-server-2008