日期:2014-05-17  浏览次数:20448 次

保留表中salary数值最小的。

create table test
(
userName varchar(30),
[month]  varchar(20),
salary   numeric(15,4)
)

insert into test values('adam','201001',4500)
insert into test values('adam','201002',3500)
insert into test values('adam','201003',2500)
go
insert into test values('alice','201001',4500)
insert into test values('alice','201002',3500)
insert into test values('alice','201003',5500)
go
insert into test values('lucy','201001',4500)
insert into test values('lucy','201002',3500)
insert into test values('lucy','201003',9500)
go
insert into test values('emily','201001',10500)
go

有没有好的删除语句,或者这种文档 学习下。
SQL

------解决方案--------------------
delete a from test a, (select userName?  min(salary) as salary from test group by username) b
where a.username=b.username and a.salary>b.salary


引用:
Quote: 引用:

delete from test where salary>(select min(salary) from test)

各个名字都要取一个

------解决方案--------------------
delete a
from test a
where exists ( select 1 from test
where username = a.username
and salary < a.salary
)
------解决方案--------------------

delete test  from test t1
 inner join (select username,min(salary)  as  salary from test group by username )
  t on t.userName = t1.userName and t1.salary > t.salary
------解决方案--------------------

create table test
(
userName varchar(30),
[month]  varchar(20),
salary   numeric(15,4)
)
 
insert into test values('adam','201001',4500)
insert into test values('adam','201002',3500)
insert into test values('adam','201003',2500)
go
insert into test values('alice','201001',4500)
insert into test values('alice','201002',3500)
insert into test values('alice','201003',5500)
go
insert into test values('lucy','201001',4500)
insert into test values('lucy','201002',3500)
insert into test values('lucy','201003',9500)
go
insert into test values('emily','201001',10500)
go


with t as
(select userName,[month],salary,
        row_number() over(partition by userName order by salary) 'rn'