请教 关于DISTINCT关键字的语句

hongpeng 2003-12-12 11:47:24
现有一表:表名TEST
有三个字段:a,b,c
里面有很多条记录,对于a字段有很多重复,我现在想取出10条不重复的记录如何处理

"select top 10 DISTINCT a,b,c from test order by c asc"这样不行,请问如何书写该语句,谢谢!
...全文
32 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
victorycyz 2003-12-12
  • 打赏
  • 举报
回复
select identity(int,1,1) as id,* into #t from test

select top 10 a.a,a.b,a.c
from #t a join
( select min(id) as nid
from #t
group by a
) b on a.id=b.nid

drop table #t
hongpeng 2003-12-12
  • 打赏
  • 举报
回复
请教txlicenhe(马可)

min(b)和min(c)在这里的作用是什么?

MIN函数不是求最小值的吗
dlpseeyou 2003-12-12
  • 打赏
  • 举报
回复
select top 10 * from test group by a
wzh1215 2003-12-12
  • 打赏
  • 举报
回复
select top 10 from (select distinct a,b,c from test order by c asc) temp
txlicenhe 2003-12-12
  • 打赏
  • 举报
回复
select top 10 a,min(b) as b,min(c) as c from test group by a
hongpeng 2003-12-12
  • 打赏
  • 举报
回复
已经搞定了,谢谢大家
结帖给分!
hongpeng 2003-12-12
  • 打赏
  • 举报
回复
请教txlicenhe(马可) 和LoveSQL(ligg) :

你们给出的方法可以通过
问题1:我还想ORDER BY C DESC 怎么处理呢,如果直接加上去是不行的
问题2:你们用的MIN,和MAX在这里是什么用的,和我解释下好么

谢谢
LoveSQL 2003-12-12
  • 打赏
  • 举报
回复
select top 10 a,max(b) as b,max(c) as c from test group by a
devilwind 2003-12-12
  • 打赏
  • 举报
回复
select top 4 a from (select distinct a from 表) a

34,590

社区成员

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

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