我可以在SQL中执行以下操作:
SELECT ROW_NUMBER() OVER (ORDER BY c.SomeField2 ASC) AS r, c.cID
FROM
(
SELECT c.SomeField1, c.SomeField2, c.SomeField3, t.SomeField1Text, p.SomeField2Text
FROM MyTable c
JOIN #TempTable1 t ON c.SomeField1 = t.SomeField1
JOIN #TempTable2 p ON c.SomeField2 = p.SomeField2
) c
但是,当我尝试对我的一个临时表中的列进行排序时,如此...
SELECT ROW_NUMBER() OVER (ORDER BY p.SomeField2Text ASC) AS r, c.cID
FROM
(
SELECT c.SomeField1, c.SomeField2, c.SomeField3, t.SomeField1Text, p.SomeField2Text
FROM MyTable c
JOIN #TempTable1 t ON c.SomeField1 = t.SomeField1
JOIN #TempTable2 p ON c.SomeField2 = p.SomeField2
) c
...我收到以下错误:
The multi-part identifier "p.SomeFieldText2" could not be bound.
你知道我为什么会收到这个错误吗?我很困惑,因为该列在我的SELECT
子句中。
答案 0 :(得分:1)
您要查询的字段名为SomeField2Text
,而不是SomeFieldText2
:
SELECT ROW_NUMBER() OVER (ORDER BY p.SomeField2Text ASC) AS r, c.cID
FROM
(
SELECT c.SomeField1, c.SomeField2, c.SomeField3, t.SomeField1Text, p.SomeField2Text
FROM MyTable c
JOIN #TempTable1 t ON c.SomeField1 = t.SomeField1
JOIN #TempTable2 p ON c.SomeField2 = p.SomeField2
) c
答案 1 :(得分:0)
您应该将子查询视为具有别名C的表。
这就是你需要通过c.SomeField2列
订购的原因SELECT ROW_NUMBER() OVER (ORDER BY c.SomeFieldText ASC) AS r, c.cID
FROM
(
SELECT c.SomeField1, c.SomeField2, c.SomeField3, t.SomeField1Text, p.SomeField2Text, p.SomeField2
FROM MyTable c
JOIN #TempTable1 t ON c.SomeField1 = t.SomeField1
JOIN #TempTable2 p ON c.SomeField2 = p.SomeField2
) c