NO NAME ...
001 WANG ...
002 YANG ...
001 ZHAO ...
002 ZHANG ...
003 LI ...
NO中有两个001和两个002,但只取一条记录,执行后:
NO NAME ...
001 WANG ...
002 YANG ...
003 LI ...
...全文
16720打赏收藏
怎样写这个sql语句?
NO NAME ... 001 WANG ... 002 YANG ... 001 ZHAO ... 002 ZHANG ... 003 LI ... NO中有两个001和两个002,但只取一条记录,执行后: NO NAME ... 001 WANG ... 002 YANG ... 003 LI ...
在MsSql中
select name,no,qty,identity(int,1,1) as id into #T1 from table1
select min(id) as id into #t2 from #t1 group by no
select t1.name,t1.no,t1.qty from #t2 t2 join #t1 t1 on t2.id=t1.id
在Oracle中
select * from table1 where rowid in (select min(rowid) from table1 group by no)
今天的高高手都哪里去了,这些问题我都能轮到我回答
select * from tablename where pk=(select min(pk) from tablename group by no)
其中的pk为该表的关键字,最好为id,对于本表来说,如果no,和name能唯一确定一行数据,则pk=name
实在不行,把数据插入一个带自动增加的identity列中,则pk=id
用一条语句写没那么简单。
要是只取重复记录中rowid最小的一条可以这样写(暂不考虑效率):
select * from tablename where rowid in (select min(rowid) from tablename where no in (select no from tablename group by no having count(*)>1) group by no
union select rowid from tablename where no in (select no from tablename group by no having count(*)=1))