为啥这里边提示order by错误!比较强的大哥们过来看看呗

haoztao 2005-05-31 10:32:44
SELECT distinct COUNT(CustomerID) AS Num, Country FROM Customers
where country in (select distinct top 3 country from Customers order by DESC)
GROUP BY Country

是不是在union中也不能用order?
...全文
87 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2005-05-31
  • 打赏
  • 举报
回复
--在union/union all中使用排序的方式如下:

--对union中的各语句使用order by
select * from(
select top 3 id from sysobjects order by id)a
union --或者union all
select * from(
select top 3 id from sysobjects order by id desc)a


--对最终结果order by
select top 3 id from syscolumns
union --或者union all
select top 3 id from sysobjects
order by id desc
hsj20041004 2005-05-31
  • 打赏
  • 举报
回复
主要是缺少了排序的列名!!!
和 “在使用UNION联合的多个查询中,只能在UNION联合的多个查询中的最后一个查询中指定排序,其他查询都不允许包含Order by子句。”无关!!。。
zjcxc 元老 2005-05-31
  • 打赏
  • 举报
回复
SELECT distinct COUNT(CustomerID) AS Num, Country FROM Customers
where country in (
select distinct top 3 country from Customers order by country DESC) --排序的列名呢?
GROUP BY Country
子陌红尘 2005-05-31
  • 打赏
  • 举报
回复
order by DESC !!!??? 按什么排序啊!?


SELECT distinct COUNT(CustomerID) AS Num, Country FROM Customers
where country in (select distinct top 3 country from Customers order by country DESC)
GROUP BY Country


是不是在union中也不能用order?
在使用UNION联合的多个查询中,只能在UNION联合的多个查询中的最后一个查询中指定排序,其他查询都不允许包含Order by子句。
haoztao 2005-05-31
  • 打赏
  • 举报
回复
OK
谢谢各位啦,呵呵!只要把那个union说清楚就可以啦!

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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