我有以下查询:
SELECT * FROM
(
SELECT
a.TeacherID, a.UniversityID,
ROW_NUMBER() OVER (ORDER BY a.TeacherID) AS RowNum
FROM
SelectAll a
LEFT JOIN
mp_Ratings r ON a.TeacherID = r.TeacherID
GROUP BY
a.TeacherID, a.UniversityID
) AS TeacherInfo
WHERE RowNum BETWEEN 10 AND 50
它工作正常。现在,如果我想做类似的事情:
SELECT * FROM
(
SELECT
a.TeacherID, a.UniversityID,
ROW_NUMBER() OVER (ORDER BY a.TeacherID) AS RowNum
FROM
SelectAll a
LEFT JOIN
mp_Ratings r ON a.TeacherID = r.TeacherID
GROUP BY
a.TeacherID, a.UniversityID
) AS TeacherInfo
WHERE RowNum BETWEEN 10 AND 50 AND UniversityID = 2
我什么也没得到,因为UniversityID = 2
从行号2991开始。我希望能够Row_Number
用每一个不同的UniversityID
.
我尝试了以下方法:
SELECT *, ROW_NUMBER() OVER (ORDER BY TeacherID) AS RowNum
FROM
(
SELECT
a.TeacherID, a.UniversityID
FROM
SelectAll a
LEFT JOIN
mp_Ratings r ON a.TeacherID = r.TeacherID
GROUP BY
a.TeacherID, a.UniversityID
) AS TeacherInfo
WHERE UniversityID = 2
这为每次搜索带来了新的集合row_numbers
,但是如果我尝试这样使用RowNum
别名:
SELECT *, ROW_NUMBER() OVER (ORDER BY TeacherID) AS RowNum
FROM
(
SELECT
a.TeacherID, a.UniversityID
FROM
SelectAll a
LEFT JOIN
mp_Ratings r ON a.TeacherID = r.TeacherID
GROUP BY
a.TeacherID, a.UniversityID
) AS TeacherInfo
WHERE UniversityID = 2
AND RowNum BETWEEN 10 AND 20
我明白了
消息 207,级别 16,状态 1,第 4 行无效的列名称“RowNum”。
我的选择有什么问题?为什么我无法访问RowNum
别名?