SELECT DISTINCT order by 同样的语句同样环境报错?特急!

qq_27341511 2017-03-09 02:30:20
报错提示:
如果指定了 SELECT DISTINCT,那么 ORDER BY 子句中的项就必须出现在选择列表中。

但很奇怪,同样的语句在同样的数据库结构里,一个执行报错,一个执行不报错!
找不到原因,难道和数据库的属性有关系吗?

大神来分析一下!! 谢谢!
...全文
479 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_27341511 2017-03-12
  • 打赏
  • 举报
回复
果然是兼容性设置问题, 设置成2008(100)就通不过,设置成2000(80)就能通过! 谢谢!!
Tiger_Zhao 2017-03-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 qq_27341511 的回复:]请教兼容级别属性在哪查? [/Quote]
数据库右键\属性\选项\兼容级别
道素 2017-03-09
  • 打赏
  • 举报
回复
distinct 某种层度就是聚合,比如比较下面两个语句

SELECT DISTINCT type FROM master.dbo.spt_values
SELECT type FROM master.dbo.spt_values GROUP BY type


tcmakebest 2017-03-09
  • 打赏
  • 举报
回复
如果使用了distinct ,那排序的字段理应在结果列中, 不然如何排序呢.
qq_27341511 2017-03-09
  • 打赏
  • 举报
回复
请教兼容级别属性在哪查?
Tiger_Zhao 2017-03-09
  • 打赏
  • 举报
回复
可以看看数据库属性中的兼容级别是否一致。
换成下面这样可以不?
ORDER BY ia_subsidiary.cvoutype,
dkeepdate,
ia_subsidiary.cvoucode,
cwhcode
qq_27341511 2017-03-09
  • 打赏
  • 举报
回复
同一个数据库下面的两个完全相同的数据实例,一个是2016年的,一个是2017年的。 不管数据怎样,一个报错一个不报错,找原因都没有方向! 各个表结构方面都是一样的,但脚本的语法是否错误应该和数据内容无关吧? 语句如下: SELECT Distinct convert(nvarchar(10),IA_Subsidiary.dKeepDate,21) as dKeepDate, convert(nvarchar(10),IA_Subsidiary.dVouDate,21) as dVouDate,enum1.enumname AS cVouchName,IA_Subsidiary.cVouCode, case when cvoutype= N'22' then N'' else (case when (0=1 and cvoutype= N'35') then N'' else cWhName end) end as cWhName, case when cvoutype= N'35' then N'' else Rd_Style.cRdName end as cRdName,IA_Subsidiary.cAccounter, IA_Subsidiary.cVouType, case when cvoutype= N'22' then N'' else (case when (0=1 and cvoutype= N'35') then N'' else IA_Subsidiary.cWhCode end) end as cWhcode, case when cvoutype= N'35' then N'' else Rd_Style.cRdCode end as cRdCode,case when cvoutype= N'35' then N'' else cDepName end as cAccDepName, case when cvoutype= N'35' then N'' else cAccDep end as cAccDepCode,case when cvoutype= N'22' then N'' else IA_Subsidiary.cDepCode end as cDepCode, case when (left(IsNull(IA_Subsidiary.cBusType, N''),2)= N'调拨' or left(IsNull(IA_Subsidiary.cBusType, N''),2)= N'组装' or left(IsNull(IA_Subsidiary.cBusType, N''),2)= N'拆卸' or left(IsNull(IA_Subsidiary.cBusType, N''),2)= N'转换') then IA_Subsidiary.cBusCode else N'' end As cBusCode, CASE WHEN (cVouType= N'24' or cVouType= N'30') then IA_Subsidiary.AutoID else ipzid end as MainID, IA_Subsidiary.id as id, enum2.enumname as cBusTypeDis,IA_Subsidiary.cSrcVouType,enum3.enumname as cWhValueStyleDis, 'Sub' As cSource, IA_Subsidiary.AutoID, IA_Subsidiary.ID, IsNull(IA_Subsidiary.cBusType, N'') As cBusType,IA_Subsidiary.psvsid as psvsid FROM (Warehouse RIGHT JOIN (department RIGHT JOIN (Rd_Style RIGHT JOIN (VouchType RIGHT JOIN IA_Subsidiary ON VouchType.cVouchType = IA_Subsidiary.cVouType) ON Rd_Style.cRdCode = IA_Subsidiary.cRdCode) ON department.cDepCode = IA_Subsidiary.cAccDep) ON Warehouse.cWhCode = IA_Subsidiary.cWhCode) Left join inventory on ia_subsidiary.cinvcode=inventory.cinvcode left join aa_enum enum1 on enum1.enumtype= N'IA.VchType' and enum1.enumcode=(case when CVOUTYPE= N'2501' then N'直运采购发票'else (CASE WHEN CVOUTYPE= N'35' THEN N'差异结转单' ELSE case when (IsNull(IA_Subsidiary.cBusType, N'')= N'假退料' and cvoutype= N'11') then N'假退料单' else VouchType.cVouchName end END) end ) and enum1.localeid= dbo.UDF_GetLocaleID() left join aa_enum enum2 on enum2.enumtype= N'IA.Bustype' and enum2.enumcode=IsNull(IA_Subsidiary.cBusType, N'') and enum2.localeid= dbo.UDF_GetLocaleID() left join aa_enum enum3 on enum3.enumtype= N'IA.ValueStyle' and enum3.enumcode=(case when cvoutype= N'22' then N'' else (case when (0=1 and cvoutype= N'35') then N'' else cWhValueStyle end) end ) and enum3.localeid= dbo.UDF_GetLocaleID() left join Rdrecords RdsCheck on RdsCheck.bCosting = 1 And RdsCheck.ID = IA_Subsidiary.iPZID And IsNull(RdsCheck.cBAccounter, N'') = N'' and Not Left(IA_Subsidiary.cVouType, 2) In (N'70', N'ex', N'05', N'06', N'5', N'6', N'25', N'26', N'27', N'28', N'29', N'20', N'21', N'22', N'35', N'24', N'30') left join PU_T_VMIUsedVouchs VmiCheck on VmiCheck.bCosting = 1 And VmiCheck.ID = IA_Subsidiary.iPZID And IsNull(VmiCheck.cBAccounter, N'') = N'' and Left(IA_Subsidiary.cVouType, 2) In (N'70') left join DispatchLists DisCheck on DisCheck.bCosting = 1 And DisCheck.DLID = IA_Subsidiary.iPZID And IsNull(DisCheck.cBAccounter, N'') = N'' and Left(IA_Subsidiary.cVouType, 2) In (N'05', N'06', N'5', N'6') left join SalebillVouchs BillCheck on BillCheck.bCosting = 1 And BillCheck.SBVID = IA_Subsidiary.iPZID And IsNull(BillCheck.cBAccounter, N'') = N'' and Left(IA_Subsidiary.cVouType, 2) In (N'26', N'27', N'28', N'29') left join purbillvouchs pusCheck on pusCheck.bCosting = 1 And pusCheck.PBVID = IA_Subsidiary.iPZID And IsNull(pusCheck.cBAccounter, N'') = N'' and Left(IA_Subsidiary.cVouType, 2) = N'25' left join JustInVouchs JustCheck on JustCheck.bCosting = 1 And JustCheck.cJVCode = IA_Subsidiary.cVouCode And IsNull(JustCheck.cBAccounter, N'') = N'' and IA_Subsidiary.cVouType In (N'20', N'21') left join ex_invoicedetail EXsCheck on EXsCheck.bCosting = 1 And EXsCheck.ID = IA_Subsidiary.iPZID And IsNull(EXsCheck.cBAccounter, N'') = N'' and Left(IA_Subsidiary.cVouType, 2) = N'ex' where IA_subsidiary.cVouType<> N'33' and IA_subsidiary.cVouType<> N'34' and ((imonth<>0 and substring(IsNull(IA_Subsidiary.cBusType, N''),1,2) = N'直运') or substring(IsNull(IA_Subsidiary.cBusType, N''),1,2) <> N'直运' or IA_subsidiary.cbustype is null) and cpzid is null And RdsCheck.ID Is Null And VmiCheck.ID Is Null And DisCheck.DLID Is Null And BillCheck.SBVID Is Null And pusCheck.ID Is Null And JustCheck.cJVCode Is Null And EXsCheck.ID Is Null AND IA_Subsidiary.cVouCode>= N'123456' AND ((Ia_Subsidiary.cVouType= N'01' and bFlag=1) or (Ia_Subsidiary.cVouType= N'01' and bFlag<>1) or (Ia_Subsidiary.cVouType= N'70' and bFlag=1) or (Ia_Subsidiary.cVouType= N'70' and bFlag<>1) or (Ia_Subsidiary.cVouType= N'10') or (Ia_Subsidiary.cVouType= N'08') or (Ia_Subsidiary.cVouType= N'26' and Ia_Subsidiary.cBusType <> N'直运销售' ) or (Ia_Subsidiary.cVouType= N'27' and Ia_Subsidiary.cBusType <> N'直运销售' ) or (Ia_Subsidiary.cVouType= N'28' and Ia_Subsidiary.cBusType <> N'直运销售' ) or (Ia_Subsidiary.cVouType= N'29' and Ia_Subsidiary.cBusType <> N'直运销售' ) or (Ia_Subsidiary.cVouType= N'32' and Ia_Subsidiary.cBusType <> N'直运销售' ) or (Ia_Subsidiary.cVouType= N'11' and Ia_Subsidiary.cBusType<> N'假退料' ) or (Ia_Subsidiary.cVouType= N'09' and Ia_Subsidiary.cBusType <> N'直运销售' ) or (Ia_Subsidiary.cVouType= N'20' and (isnull(iAInPrice,0)<>0 or isnull(iDebitDifCost,0)<>0 or isnull(iCreditDifCost,0)<>0)) or (Ia_Subsidiary.cVouType= N'21') or (Ia_Subsidiary.cVouType= N'22') or (Ia_Subsidiary.cVouType= N'35') or (Ia_Subsidiary.cVouType= N'24') or (Ia_Subsidiary.cVouType= N'30' and bFlag=1) or (Ia_Subsidiary.cVouType= N'30' and bFlag<>1) or (Ia_Subsidiary.cVouType= N'05' and Ia_Subsidiary.cBusType= N'分期收款' and imonth <> 0) or (Ia_Subsidiary.cVouType= N'05') or (Ia_Subsidiary.cVouType= N'06') or (Ia_Subsidiary.cVouType= N'AA') or (Ia_Subsidiary.cVouType= N'AA') or (Ia_Subsidiary.cVouType= N'27' and not iOutCost is null and imonth <> 0 and not Ia_Subsidiary.cBusType in (N'分期收款',N'委托',N'直运销售') ) or (Ia_Subsidiary.cVouType= N'27' and not iOutCost is null and imonth <> 0 and not Ia_Subsidiary.cBusType in (N'分期收款',N'委托',N'直运销售') ) or (Ia_Subsidiary.cVouType= N'66') or (Ia_Subsidiary.cVouType= N'66') or (Ia_Subsidiary.cVouType Like N'25%' and Ia_Subsidiary.cBusType= N'直运采购' and imonth <> 0) or (Ia_Subsidiary.cVouType= N'25') or (Ia_Subsidiary.cVouType in ( N'26',N'27') and Ia_Subsidiary.cBusType= N'直运销售' and imonth <> 0 ) or (Ia_Subsidiary.cVouType In ( N'EX26',N'EX27') and Ia_Subsidiary.cBusType <> N'直运销售' ) or (Ia_Subsidiary.cVouType in ( N'EX26',N'EX27') and Ia_Subsidiary.cBusType= N'直运销售' and imonth <> 0 ) or (Ia_Subsidiary.cVouType= N'11' and Ia_Subsidiary.cBusType= N'假退料' ) or (Ia_Subsidiary.cVouType= N'61')) and ( ((Not warehouse.cwhcode IN (Select cWhCode From Warehouse Where bInCost = 0)) or warehouse.cwhcode is null) Or warehouse.cwhcode Is Null) Order by IA_Subsidiary.cVouType,IA_Subsidiary.dKeepDate,IA_subsidiary.cVoucode,IA_Subsidiary.cWhCode
二月十六 2017-03-09
  • 打赏
  • 举报
回复
不能够,贴上来一下数据结构和你的语句让大家看看
卖水果的net 2017-03-09
  • 打赏
  • 举报
回复
不会的,分别说一下两个库的版本。

22,302

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧