是否可以按row_number内的新列排序?我想这样做:
select text1 + text2 as NEW_TEXT, row_number over (order by NEW_TEXT) from table
,但这样的排序不起作用(我得到随机结果)。
必须重复表达式text1 + text2
select text1 + text2 as NEW_TEXT,
row_number() over (order by text1 + text2)
from table
或者分步骤执行(foo在这里可以是CTE)
SELECT
NEW_TEXT,
row_number() over (order by NEW_TEXT)
FROM
(
select text1 + text2 as NEW_TEXT from table
) foo
另一种选择是将初始结果包装在子选择中,并对该子选择应用ROW_NUMBER
。
SELECT NEW_TEXT
, ROW_NUMBER() OVER (ORDER BY NEW_TEXT)
FROM (
SELECT text1 + text2 AS NEW_TEXT
FROM [table]
) q
测试数据;WITH [table] (text1, text2) AS (
SELECT '1', '1'
UNION ALL SELECT '1', '0'
UNION ALL SELECT '2', '2'
UNION ALL SELECT '2', '1'
)
SELECT NEW_TEXT
, ROW_NUMBER() OVER (ORDER BY NEW_TEXT)
FROM (
SELECT text1 + text2 AS NEW_TEXT
FROM [table]
) q
你可以这样使用子查询:
select NEW_TEXT, row_number() over (order by NEW_TEXT)
from
(
select Text1 + Text2 as NEW_TEXT
from TestTable
) TempTable
select text1 + text2 as NEW_TEXT,
row_number over (order by text1 + text2)
from table