Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在子查询中使用ORDER BY_Sql_Sql Server_Sql Server 2008_Tsql - Fatal编程技术网

Sql 如何在子查询中使用ORDER BY

Sql 如何在子查询中使用ORDER BY,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,运行此代码时,我会收到错误消息: Msg 1033,15级,状态1,第1行ORDER BY子句在中无效 视图、内联函数、派生表、子查询和公共表 表达式,除非还指定了TOP、OFFSET或FOR XML 我可以编写另一个与上述查询完全相同的查询吗?您的查询存在的问题不仅仅是没有采样的排序。例如,当您在()中的IN谓词中使用子查询时,它不能返回超过1列,而您的查询返回两列 看看这个,也许我猜对了: update dbo.Sheet1$ set F01 = 0 where ID in( select

运行此代码时,我会收到错误消息:

Msg 1033,15级,状态1,第1行ORDER BY子句在中无效 视图、内联函数、派生表、子查询和公共表 表达式,除非还指定了TOP、OFFSET或FOR XML


我可以编写另一个与上述查询完全相同的查询吗?

您的查询存在的问题不仅仅是没有采样的排序。例如,当您在()中的
IN
谓词中使用子查询时,它不能返回超过1列,而您的查询返回两列

看看这个,也许我猜对了:

update dbo.Sheet1$ set F01 = 0 where ID in(
select top 3 ID from dbo.Sheet1$ where ID in(
select ID, ISNULL(F01,0) + ISNULL(F02,0) + ISNULL(F03,0) as RowSum 
   from dbo.Sheet1$ where F01 = 1 AND F02 = 1 order by RowSum desc))

错误的2个可能原因是:

1) 子查询具有这样的条件:在子查询中尝试查找2列,而在中查询时只能有1列

2) 您的子查询不能使用Order By,因为无论您是否要排序,它都将从子查询结果集中查找匹配的记录并更新这些记录。因此,在子查询中使用orderby没有任何意义。无论您是在子查询中保留Order By还是删除Order By,它都会提供相同的输出


希望这有帮助

使用子查询中的top select可以解决您的问题
选择top 2147483647
可能重复的
update dbo.Sheet1$ set F01 = 0
where ID in (
    select top 3 ID
    from dbo.Sheet1$
    where F01 = 1 AND F02 = 1
    order by ISNULL(F01,0) + ISNULL(F02,0) + ISNULL(F03,0) desc
);