在以下示例中,无法按Field_2(子查询)进行排序 有解决办法吗?
SELECT * FROM ( SELECT Field_A, (SELECT Field_1 FROM Table_2) AS Field_B, Field_C, ROW_NUMBER() OVER (ORDER BY Field_B ASC) AS Row_Num FROM Table_1 ) AS Name WHERE Row_Num BETWEEN 1 AND 100
答案 0 :(得分:3)
您不能在与声明相同的select子句中使用别名。
您可以按照Zaynul Abadin Tuhin的答案中的说明重复子查询,也可以使用另一个派生表(或cte):
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY Field_B ASC) AS Row_Num
FROM
(
SELECT
Field_A,
(SELECT Field_1 FROM Table_2) AS Field_B,
Field_C
FROM Table_1
) AS Name
) As NumberedName
WHERE Row_Num BETWEEN 1 AND 100
另外,请注意,除非Table_2仅一行,否则将(SELECT Field_1 FROM Table_2)
用作子查询将导致错误-
子查询返回了多个值。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。
Larnu在评论中指出。
答案 1 :(得分:0)
您不能使用别名,必须按顺序使用该子查询
with cte as (
SELECT
Field_A,
(SELECT Field_1 FROM Table_2
) AS Field_B, --- it should have to rerun 1 value otherwise it will thrown error , so i think here need where condition
Field_C
FROM Table_1
) , cte2 as
( select *, row_number() over(order by Field_B asc) rn
from cte
) select * from cte where rn>=1 and rn<=100