日期:2014-05-17 浏览次数:20707 次
create table tabletest_1
(
Sno varchar(10),
Cno varchar(10),
Score int
)
delete from tabletest_1
insert into tabletest_1 values ('001','A1' ,'20');
insert into tabletest_1 values ('001','A2' ,'30');
insert into tabletest_1 values ('001','A3' ,'40')
insert into tabletest_1 values ('001','B1' ,'20')
insert into tabletest_1 values ('001','Z1' ,'5')
insert into tabletest_1 values ('002','A1' ,'20')
insert into tabletest_1 values ('002','B1' ,'30')
insert into tabletest_1 values ('002','Z1' ,'5')
insert into tabletest_1 values ('003','B1' ,'40')
insert into tabletest_1 values ('003','Z1' ,'5')
--想把Z1的成绩加到A系列成绩最高的那条记录上,然后如果没有A系列成绩的话,加到B1上
--想要的结果是
select Sno,Cno,score,
case when exists (select MAX(Cno) from tabletest_1 I1 where I1.Sno=O.Sno and Cno like 'A%')
then
(select MAX(Score) from tabletest_1 I2 where I2.Sno=O.Sno and Cno like 'A%')
+
(select Score from tabletest_1 I2 where I2.Sno=O.Sno and Cno = 'Z1' )
else
(select MAX(Score) from tabletest_1 I2 where I2.Sno=O.Sno and Cno = 'B1')
+
(select Score from tabletest_1 I2 where I2.Sno=O.Sno and Cno = 'Z1' )
end as score2
fr