Sql server 2012 使用select distinct关于SQL Server order by
嗨,我在这个查询中出错了Sql server 2012 使用select distinct关于SQL Server order by,sql-server-2012,Sql Server 2012,嗨,我在这个查询中出错了 select distinct convert(varchar (12),date,105) as Mydate,count(itemcount) from Productiontbl group by date order by cast(run_date as date) desc 错误:-如果选择,则“按项目排序”必须出现在“选择”列表中 指定了DISTINCT 该错误表示您的选择DISTINCT未返回与您在订单中所指示的内容
select
distinct convert(varchar (12),date,105) as Mydate,count(itemcount)
from
Productiontbl
group by
date
order by
cast(run_date as date) desc
错误:-如果选择,则“按项目排序”必须出现在“选择”列表中
指定了DISTINCT
该错误表示您的
选择DISTINCT
未返回与您在订单中所指示的内容类似的内容。如果您的SELECT DISTINCT
列表中没有按cast排序(run_date as date)
,则无法执行ORDER BY cast。正如错误所说,“如果指定SELECT DISTINCT,则按项目排序必须出现在选择列表中。”
因此,您的一列将(varchar(12),date,105)转换为Mydate
或count(itemcount)如果要在查询中使用Order by子句,则必须按Order by子句指定。请参阅下面的链接并了解查询执行的顺序
您正试图按选择中不存在的列排序