sql语句问题 - MS-SQL Server / 应用实例
有一个表,存放的是学生的成绩,
完成如下功能:
如果一个学生的某门课程的成绩低于该门课程的平均成绩,将他的成绩提高5%
s_c(sid,cid,score)
sid学号
cid 课程号
score成绩
SQL code:
update s set s.score=1.05*score
from s_c s
join
(select cid,avg(score) as avgscore
from s_c
Group by cid) c on s.cid=c.cid and s.score<c.avgscore
SQL code:
create table s_c(sid int,cid int,score int)
insert s_c
select 1,1,40 union all
select 2,1,50 union all
select 3,1,60 union all
select 1,2,50 union all
select 2,2,60 union all
select 3,2,70
select * from s_c t
where score<(select avg(score) from s_c where cid=t.cid group by cid)
/*
sid cid score
----------- ----------- -----------
1 1 40
1 2 50
(所影响的行数为 2 行)*/
update t set score = score*1.05
from s_c t
where score<(select avg(score) from s_c where cid=t.cid group by cid)
select * from s_c
/*
sid cid score
----------- ----------- -----------
1 1 42
2 1 50
3 1 60
1 2 52
2 2 60
3 2 70
(所影响的行数为 6 行)
*/
相关问答:
1。怎样使xp_cmdshell能完整输出超过255个字符的字符串。
2。select 时,检索速度是与from后的 TABLE顺序有关,还是与where条件的顺序有关(TABLE数据多少 )
在系统属性设定里有个选项,可以修改单字段输出字数限制. ......
id url rank ......
我想查询出每天数据的最大的一个值。表的格式如下
表名: hisdata
字段 编号 值 状态 时间
Id value state dattime
101 32.3 0 ......
需求如下:
学院 academy(aid,aname)
班级 class(cid,cname,aid)
学生 stu(sid,sname,aid,cid)
住宿区 region(rid,rname)
宿舍楼 build(bid,rid,bnote) bnote是‘男’/‘女’
宿舍 dorm(did,rid,bid,bedn ......