首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请问一分组的有关问题

2012-08-31 
请教一分组的问题表里有数据比如:idname ip1 a 102 a 343 b 564 c 235 b 246 d 347 d 25请问如何只把a b c

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

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]);

热点排行