日期:2014-05-18  浏览次数:20418 次

请教一个SQLServer2000查询
有下列数据:
class     name         other
0001       姓名1         备注1
0001       姓名2         备注2
0002       姓名3         备注3
0003       姓名4         备注4
0004       姓名5         备注5
0004       姓名6         备注6
....       .....         .....

我想查询的结果是,将上面class字段重复的记录只取第一个,获取下面的得到结果集:
0001       姓名1         备注1
0002       姓名3         备注3
0003       姓名4         备注4
0004       姓名5         备注5
....       .....         .....

请问,我应该怎么样查询呢?谢谢!




------解决方案--------------------
select * from table where class in(select min(class) from table group by class)
------解决方案--------------------
假设name是唯一的

select t.* from t inner join (select class,min(name) as name from t group by class) a on t.class=a.class and t.name=a.name

*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)

最新版本:20070130

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
------解决方案--------------------


select class, name=min(name), other=min(other)
from T
group by class
------解决方案--------------------

select * from 表 a
where not exists (select 1 from 表 where class=a.class and name <a.name)

------解决方案--------------------
create table T(class varchar(10), name varchar(10), other varchar(10))
insert T select '0001 ', '姓名1 ', '备注1 '
union all select '0001 ', '姓名2 ', '备注2 '
union all select '0002 ', '姓名3 ', '备注3 '
union all select '0003 ', '姓名4 ', '备注4 '
union all select '0004 ', '姓名5 ', '备注5 '
union all select '0004 ', '姓名6 ', '备注6 '

select class, name=min(name), other=min(other)
from T
group by class

--result
class name other
---------- ---------- ----------
0001 姓名1 备注1
0002 姓名3 备注3
0003 姓名4 备注4
0004 姓名5 备注5

(4 row(s) affected)