去重的问题

如表:
q1 q2 q3 q4
1 w e s
2 w d f
5 e e f
3 d d s
4 r h j
1 g e s
5 f d d
7 l p j
结果要:
q1 q2 q3 q4
1 w e s
2 w d f
3 d d s
4 r h j
5 e e f
7 l p j
[252 byte] By [wth1150-@疯癫行者$] at [2008-4-24]
# 1
select id = identity(int,1,1),* into #tmp from table

select q1,q2,q3,q4 from #tmp a where
not exists(select 1 from #tmp where q1 = a.q1 and id < a.id)
hellowork-一两清风 at 2007-10-22 > top of Msdn China Tech,MS-SQL Server,应用实例...
# 2
楼上算一种
还有其它办法,用having
yuehaiyang at 2007-10-22 > top of Msdn China Tech,MS-SQL Server,应用实例...
# 3
CREATE TABLE # (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[q1] [int] NULL ,
[q2] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[q3] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[q4] [char] (10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
select * from #
insert # (q1,q2,q3,q4)
select * from a

select #.q1,#.q2,#.q3,#.q4 from # join(
select min(id) as id,q1 from # group by q1) b on #.id=b.id
drop table #
jaway-无限渴望 at 2007-10-22 > top of Msdn China Tech,MS-SQL Server,应用实例...
# 4
题目没有说清楚,其实结果可能不唯一的.id为5的到底去哪条,随机删除一条,留一条?
xzq111 at 2007-10-22 > top of Msdn China Tech,MS-SQL Server,应用实例...
# 5
select * from del_person_temp a
where id=(select top 1 id from del_person_temp where comp_nme=a.comp_nme order by id)

select * from del_person_temp where id in(select min(id) from del_person_temp group by comp_nme)
select min(id) from del_person_temp group by comp_nme
wth1150-@疯癫行者$ at 2007-10-22 > top of Msdn China Tech,MS-SQL Server,应用实例...