子查询中 Order By 的 SQL 错误 [英] SQL Error with Order By in Subquery

查看:44
本文介绍了子查询中 Order By 的 SQL 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SQL Server 2005.

I'm working with SQL Server 2005.

我的查询是:

SELECT (
  SELECT COUNT(1) FROM Seanslar WHERE MONTH(tarihi) = 4
  GROUP BY refKlinik_id
  ORDER BY refKlinik_id
) as dorduncuay

和错误:

ORDER BY 子句在视图、内联函数、派生函数中无效表、子查询和公用表表达式,除非 TOP 或 FOR还指定了 XML.

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.

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

推荐答案

这是你得到的错误(重点是我的):

This is the error you get (emphasis mine):

ORDER BY 子句在以下情况下无效视图,内联函数,派生表、子查询和公用表表达式,除非 TOP 或 FOR XML 是也指定了.

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.

那么,你怎样才能避免这个错误呢?通过指定 TOP,我猜是一种可能性.

So, how can you avoid the error? By specifying TOP, would be one possibility, I guess.

SELECT (
  SELECT TOP 100 PERCENT
  COUNT(1) FROM Seanslar WHERE MONTH(tarihi) = 4
  GROUP BY refKlinik_id
  ORDER BY refKlinik_id
) as dorduncuay

这篇关于子查询中 Order By 的 SQL 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆