首页 > 数据库 >mssql排序order by42000报错解决

mssql排序order by42000报错解决

时间:2024-03-04 23:55:46浏览次数:21  
标签:cnt by42000 查询 报错 date order select

原文链接:https://blog.csdn.net/wang1qqqq/article/details/122961882

在mssql查询中,如果子查询中使用order by,会出现报错:[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]除非另外还指定了 TOP、OFFSET 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。
如下语法形式会出现报错:

select * from (
    select c_date,count(*) cnt from tablename
    order by c_date
) res

 此时是因为order by 用在了子查询中,所以出现报错。

解决方法①:

如果你的实际应用场景允许把order by用在子查询外,也能达到预期效果,就把order by功能放在外层或最外层,即可解决此问题。

select * from (
    select c_date,count(*) cnt from tablename
) res
order by c_date

  

解决方法②:

实际应用时有复杂的sql、子查询必须进行order by的场景,此时搭配top 100 percent(保留100%)进行使用,也可以解决问题

select * from (
    select top 100 percent c_date,count(*) cnt from tablename
    order by c_date
) res    

  

 

标签:cnt,by42000,查询,报错,date,order,select
From: https://www.cnblogs.com/Dongmy/p/18053060

相关文章

  • SQL报错:order by不能直接出现在union的子句中
    原文链接:https://www.cnblogs.com/xuwinwin/p/15877703.html1、报错写法:SELECT*FROMt1ORDERBYmonthASCUNIONSELECT*FROMt2ORDERBYmonthASC2、原因与解决办法:orderby不能直接出现在union的子句中,但是可以出现在子句的子句中。可以通过两个查询分别加括号......
  • PostgreSQL 在使用连表语句时报错 ERROR: operator does not exist: bigint = charact
    背景在使用PostgreSQL数据库过程中,使用了连表语句如下所示,其中a表的order_no为bigint类型,b表的order_no为varchar类型select*fromtable_orderainnerjointable_order_itembona.order_no=b.order_no;遇到提示:ERROR:operatordoesnotexist:bigint=characterv......
  • vite+vue3 遇到报错 Uncaught SyntaxError: Cannot use import statement outside a m
    按照报错找到了对应的位置import{createApp}from'/node_modules/.vite/deps/vue.js?v=d0a669cf'importAppfrom'/src/pages/project1/App.vue'//import'./index.css'//importrouterfrom"./router"//createApp(App).mount(&#......
  • 关于SAP-APP机器-R3trans -d报错-R3trans: /lib64/libstdc++.so.6: version `GLIBCXX_
    在SAP-应用-APP-机器上执行如下命令报错awpxxx03:prdadm270>R3trans-dR3trans:/lib64/libstdc++.so.6:version`GLIBCXX_3.4.26'notfound(requiredbyR3trans) 其实之前,使用过一种方法解决这个问题,可以参考笔者另一篇文章《关于Redhat-Linux中-compat-sap-c++的说......
  • pip install selenium报错 raise ReadTimeoutError--解决方法
    pipinstallselenium报错如下:raiseReadTimeoutError(self._pool,None,"Readtimedout.")pip._vendor.urllib3.exceptions.ReadTimeoutError:HTTPSConnectionPool(host='files.pythonhosted.org',port=443):Readtimedout. 原因:timeout超时,默认15s解......
  • nacos报错
    1.nacos日志报错com.alibaba.nacos.api.exception.runtime.NacosRuntimeException:ErrCode:500,ErrMsg:Userlimitofinotifywatchesreached解决:这个错误是由于Linux中inotify观察者数量达到上限导致的。在Linux系统中,inotify是用来监视文件系统事件的机制,当监视的文......
  • JSON.parse解析字符串报错-SyntaxError: Unexpected token ‘ in JSON at position 报
    “SyntaxError:Unexpectedtoken’inJSONatposition”报错原因是因为解析的字符串对象中,JSON.parse无法识别;JSON.parse可以将标准的json类型数据转换为JavaScript对象,如果数据不是正确的json类型的数据则会控制台报错,可能会阻断代码的正常运行我们可以写一个函数来......
  • shell工具连接linux时的报错问题
    问题描述在使用shell工具连接linux时报以下错误SSH!Agentauthselected,butnorunningagentisdetectedSSH!Agentauthselected,butnorunningagentisdetected解决方法方法1#首先使用ping方法查看是否在同一网段pingxxx.xxx.xx.x方法2#安装[openss......
  • pytorch报错:Variable._execution_engine.run_backward( # Calls into the C++ engine
    GPU模式下运行pytorch代码报错,pytorch为2.2.1,NVIDIA驱动版本535.161.07File"/home/devil/anaconda3/envs/sample-factory/lib/python3.11/site-packages/torch/_tensor.py",line522,inbackwardtorch.autograd.backward(File"/home/devil/anaconda3/envs/sample-......
  • 解决ssh链接报错问题
    在使用ssh连接时出现报错,如下┌──(kali㉿kali)-[~]└─[email protected]:nomatchinghostkeytypefound.Theiroffer:ssh-rsa,ssh-dss意思就是找不到匹配的主机密钥类型,需要手动......