最近去面试发现要手写存储过程,表结构如下,表名num:
id | name | num |
1 | aaa | 23 |
2 | bbb | 31 |
3 | bbb | 56 |
4 | ccc | 64 |
5 | ccc | 21 |
6 | ccc | 53 |
1.要求name相同之躯一条记录
select * from num a where not exists(select 1 from num where name=a.name and id<a.id)
or
select min([id]),[name],max([num]) from num group by [name]
2.要求删除多余的记录
select min([id]) ]as [id],[name],max([num]) as [num] into newnum from num group by [name]
drop num
exec sp_rename 'newnum','num'
可惜如此简单的存储过程当时手写写不出来,真是郁闷,哥们编程对环境依赖太大了,郁闷中。。。。。。