如何在一个表中搜索出相同字段的第一条记录
表名是table1,其中有个字段account,我现在想做的就是如何把表中所有不同的account的第一条记录查询出来,这个语句该怎么写? 问题点数:20、回复次数:9Top
1 楼flyeq008()回复于 2006-03-01 10:26:45 得分 0
select top 1 account from table1
group by account
having count(account)>1Top
2 楼xiaoduo(多多)回复于 2006-03-01 10:32:04 得分 0
这个table1中,我需要查询出来的结果除了account这个字段外,还需要包括其它的字段比如timeTop
3 楼wood111(木头一根)回复于 2006-03-01 10:52:24 得分 0
select * from table1 where account in (
select top 1 account from table1
group by account
having count(account)>1
)Top
4 楼msjqd(黑色幽默)回复于 2006-03-01 12:52:30 得分 0
select A.*
from test A
where name = (
select top 1 name from test where dis = A.dis
)Top
5 楼msjqd(黑色幽默)回复于 2006-03-01 12:54:02 得分 0
create table test
(
dis varchar(20),
name varchar(200),
count int
)
insert into test select '110000', '北京市', 10
insert into test select '110000', '北京市朝阳区', 20
insert into test select '110000', '北京市海啶区',20
insert into test select '120000', '天津市' ,8
insert into test select '120000', '天津市**区' ,6
insert into test select '120031', '天津市**区' , 10
insert into test select '420000', '湖北省' ,3000
insert into test select '420000', '湖北省武汉市', 2000
insert into test select '420200', '湖北省武汉市江岸区', 300
insert into test select '420200', '湖北省武汉市武昌区', 200
select dis
from test
group by dis
having count(dis) > 1
select A.*
from test A
where name = (
select top 1 name from test where dis = A.dis
)
drop table test
dis name count
110000 北京市 10
120000 天津市 8
120031 天津市**区 10
420000 湖北省 3000
420200 湖北省武汉市江岸区 300
Top
6 楼msjqd(黑色幽默)回复于 2006-03-01 12:55:02 得分 0
sorry
select dis
from test
group by dis
having count(dis) > 1
是错误的!测试时没有删除Top
7 楼sxlcom(木头)回复于 2006-03-01 13:05:01 得分 0
select
t.*
from
表1 t
where
t.ss in(select ss from 表1 group by ss having count(*) > 1)Top
8 楼mislrb(上班看看早报,上上CSDN,下班看看电影)回复于 2006-03-01 13:14:48 得分 0
--如果有ID字段的话(id int identity(1,1)
select * from yourtable t
where not exists(select 1 from yourtable where id<t.id and account=t.account)
--如果没有
select id=identity(int,1,1),* into #t from yourtable
select * from #t t
where not exists(select 1 from #t where id<t.id and account=t.account)
Top
9 楼lgjlry(455)回复于 2006-03-01 17:33:27 得分 0
学习Top




