34,576
社区成员
发帖
与我相关
我的任务
分享
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
delete test from test inner join
(
select userName,min(salary)salary from test group by userName
)d
on test.userName = d.userName and test.salary > d.salary
select userName,
count(1) '次数',
sum(salary) '汇总'
from test
group by userName
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'
from test
)
delete from t where rn>1
select userName,[month],salary from test
/*
userName month salary
------------------------------ -------------------- ---------------------------------------
adam 201003 2500.0000
alice 201002 3500.0000
lucy 201002 3500.0000
emily 201001 10500.0000
(4 row(s) affected)
*/
delete from test where salary not in
(
select min(salary) from test group by userName
)