“列名无效”ORDER BY

时间:2015-11-17 15:01:01

标签: sql

当我运行我的查询时,我收到以下消息:

Invalid column name 'column1'

查询:

SELECT TOP 1 * From (
(select Top 2 convert(datetime,column1,101) AS TEST from table1 ORDER BY column1 DESC) X                   
ORDER BY column1)

3 个答案:

答案 0 :(得分:0)

SELECT TOP 1 TEST 
From (
       SELECT Top 2 
            convert(datetime,column1,101) AS TEST,
            column1         // <- include column1
       FROM  table1 
       ORDER BY column1 DESC
     ) X                   
ORDER BY column1 ASC

答案 1 :(得分:0)

更改&#39;转换(datetime,column1,101)AS TEST&#39;转换(datetime,column1,101)AS column1&#39;

答案 2 :(得分:0)

也许最简单的方法是将转换移出子查询:

SELECT TOP 1 convert(datetime, column1, 101) as test
From (select Top 2 t.*
      from table1 
      order by column1 desc
     ) X                   
ORDER BY column1;

或者,如果您的数据库支持它,请使用ANSI标准FETCH / OFFSET

select convert(datetime, column1, 101) as test
from table1
order by column1 desc
offset 1 row
fetch first 1 row only;