日期:2014-05-17  浏览次数:20347 次

请教一分组的问题
表里有数据比如:

id name ip
1 a 10
2 a 34
3 b 56
4 c 23
5 b 24
6 d 34
7 d 25

请问如何只把a b c d 查出来?把重复的去掉,比如name为a的记录,任取一条就可以。
谢谢

------解决方案--------------------
if object_id('test') is not null drop table test
go
create table test(id int,name varchar(5),ip int)
go
insert into test
select 1, 'a', 10 union all
select 2, 'a', 34 union all
select 3, 'b', 56 union all
select 4, 'c', 23 union all
select 5, 'b', 24 union all
select 6, 'd', 34 union all
select 7, 'd', 25
go


select * from test a where id=(select top 1 id from test where name=a.name )
------解决方案--------------------
SELECT * FROM tbl_temp WHERE serial IN (SELECT min(serial) FROM tbl_temp GROUP BY [name]);