有一表t含有 id, name(string), score(int), mapping_name(string), flag(bool)
其中name各不相同,每一个name映射到一个mapping_name, 映射得分用score来表示(一个mapping_name可以对应多个name), flag域初始全为false。
要求完成以下功能:
1. 选出所有映射到相同mapping_name的name(设此集合为N)中score最大的name,并更新相应的flag为true,相应的score值为集合N中score的和。
2. 删除所有flag值为false的行。
例如:t=
id name(string), score(int), mapping_name(string), flag(bool)
1 f1 20 f false
2 f2 40 f false
3 f3 60 f false
4 g1 10 g false
5 g2 15 g false
6 g3 17 g false
After first step:t=
id name(string), score(int), mapping_name(string), flag(bool)
1 f1 20 f false
2 f2 40 f false
3 f3 120 f true
4 g1 10 g false
5 g2 15 g false
6 g3 42 g true
After second step:t=
id name(string), score(int), mapping_name(string), flag(bool)
1 f3 120 f true
2 g3 42 g true
要求:不能使用任何中间表,只在同一表中操作
求此SQL语句,越简单,效率越高越好
谢谢!!
5 个解决方案
#1
试试
#2
update t t1 set t1.score=
(select sum(score),mapping_name from t t2
group by t2.mapping_name where t1.mapping_name=t2.mapping_name),
t1.flage=true
where (t1.score,t1.mapping_name) in
(select max(score),mapping_name from t t3 group by mapping_name)
delete from t where t.flag=false;
commit;
--没有测过...
(select sum(score),mapping_name from t t2
group by t2.mapping_name where t1.mapping_name=t2.mapping_name),
t1.flage=true
where (t1.score,t1.mapping_name) in
(select max(score),mapping_name from t t3 group by mapping_name)
delete from t where t.flag=false;
commit;
--没有测过...
#3
Update t
set flag=true
from t
Inner Join (select mapping_name,max(score) as score from t group by mapping_name) tb
t.mapping_name=tb.mapping_name and t.score =tb.score
go
delete t where flag=false.
go
#4
Update t
set flag=true
from t
Inner Join (select mapping_name,max(score) as score from t group by mapping_name) tb
on t.mapping_name=tb.mapping_name and t.score=tb.score
go
delete t where flag=false.
go
上面少了个 On
#5
update t
set score=
(select sum(score) from t t2
where t2.mapping_name=t.mapping_name group by t2.mapping_name ),
flag='true'
where id in
(select id from t t1 inner join
(select max(score)score ,mapping_name from t
group by mapping_name) t3
on t1.score=t3.score and t1.mapping_name=t3.mapping_name )
delete from t where t.flag=false;
commit;
--这个测过了
set score=
(select sum(score) from t t2
where t2.mapping_name=t.mapping_name group by t2.mapping_name ),
flag='true'
where id in
(select id from t t1 inner join
(select max(score)score ,mapping_name from t
group by mapping_name) t3
on t1.score=t3.score and t1.mapping_name=t3.mapping_name )
delete from t where t.flag=false;
commit;
--这个测过了
#1
试试
#2
update t t1 set t1.score=
(select sum(score),mapping_name from t t2
group by t2.mapping_name where t1.mapping_name=t2.mapping_name),
t1.flage=true
where (t1.score,t1.mapping_name) in
(select max(score),mapping_name from t t3 group by mapping_name)
delete from t where t.flag=false;
commit;
--没有测过...
(select sum(score),mapping_name from t t2
group by t2.mapping_name where t1.mapping_name=t2.mapping_name),
t1.flage=true
where (t1.score,t1.mapping_name) in
(select max(score),mapping_name from t t3 group by mapping_name)
delete from t where t.flag=false;
commit;
--没有测过...
#3
Update t
set flag=true
from t
Inner Join (select mapping_name,max(score) as score from t group by mapping_name) tb
t.mapping_name=tb.mapping_name and t.score =tb.score
go
delete t where flag=false.
go
#4
Update t
set flag=true
from t
Inner Join (select mapping_name,max(score) as score from t group by mapping_name) tb
on t.mapping_name=tb.mapping_name and t.score=tb.score
go
delete t where flag=false.
go
上面少了个 On
#5
update t
set score=
(select sum(score) from t t2
where t2.mapping_name=t.mapping_name group by t2.mapping_name ),
flag='true'
where id in
(select id from t t1 inner join
(select max(score)score ,mapping_name from t
group by mapping_name) t3
on t1.score=t3.score and t1.mapping_name=t3.mapping_name )
delete from t where t.flag=false;
commit;
--这个测过了
set score=
(select sum(score) from t t2
where t2.mapping_name=t.mapping_name group by t2.mapping_name ),
flag='true'
where id in
(select id from t t1 inner join
(select max(score)score ,mapping_name from t
group by mapping_name) t3
on t1.score=t3.score and t1.mapping_name=t3.mapping_name )
delete from t where t.flag=false;
commit;
--这个测过了