通过一个子查询得到学生的SNo,SName,平均成绩,将这个结果插入到表score,这个sql语句怎么写??

q191727779 2012-03-19 09:23:43
求下面的SQL语句

要求:通过一个子查询得到学生的学号、姓名、平均成绩,将这个结果插入到表score,其中平均成绩是要算出来的,以下是有关联的表

表SC
SNo CNo SDate Score
103 3-245 2009-05-01 86
105 3-245 2009-06-11 75
109 3-245 2010-06-23 68
103 3-105 2008-10-11 92
105 3-105 2010-10-14 88
109 3-105 2010-10-15 76
101 3-105 2009-10-20 64
107 3-105 2009-10-17 88
108 3-105 2010-10-18 78
101 6-166 2008-05-18 85
107 6-166 2008-06-21 79
108 6-166 2009-06-08 81

Student
SNo SName Sex BirthDate Class Email
108 曾华 男 1982-09-01 95033 zengh@sohu.com
105 匡明 男 1982-10-02 95031 kuangm@sina.com.cn
107 王丽 女 1981-01-23 95033 wangl@hotmail.com
101 李军 男 1983-02-20 95033 lij@163.com
109 王芳 女 1982-02-10 95031 wangf@yahoo.com
103 陆君 男 1980-06-03 95031 luj@263.com

...全文
706 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
q191727779 2012-03-19
  • 打赏
  • 举报
回复
亲,还是不行哦,10楼的就可以,不过,thanks all the same~~~[Quote=引用 9 楼 acherat 的回复:]
SQL code

select a.sno,a.sname,avg(b.score) score into #tb
from student a join SC b on a.sno = b.sno
--where ...
group by a.sno,a.sname

insert into score(sno,sname,score)
select *
from #tb t……
[/Quote]
q191727779 2012-03-19
  • 打赏
  • 举报
回复
thx~~~[Quote=引用 10 楼 z81434362 的回复:]
SQL code

insert into score(sno,sname,score)
select sno,sname,(select avg(score) from sc where sc.SNo=student.SNo)
from student
[/Quote]
q191727779 2012-03-19
  • 打赏
  • 举报
回复
thx~~~[Quote=引用 10 楼 z81434362 的回复:]
SQL code

insert into score(sno,sname,score)
select sno,sname,(select avg(score) from sc where sc.SNo=student.SNo)
from student
[/Quote]
elegant87 2012-03-19
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 acherat 的回复:]

SQL code

select a.sno,a.sname,avg(b.score) score into #tb
from student a join SC b on a.sno = b.sno
--where ...
group by a.sno,a.sname

insert into score(sno,sname,score)
select *
from #tb t
where ……
[/Quote]
这个没有问题的
五更琉璃 2012-03-19
  • 打赏
  • 举报
回复

insert into score(sno,sname,score)
select sno,sname,(select avg(score) from sc where sc.SNo=student.SNo)
from student
AcHerat 元老 2012-03-19
  • 打赏
  • 举报
回复

select a.sno,a.sname,avg(b.score) score into #tb
from student a join SC b on a.sno = b.sno
--where ...
group by a.sno,a.sname

insert into score(sno,sname,score)
select *
from #tb t
where not exists (select 1 from score where sno = t.sno)

update a
set a.score = b.score
from score a join #tb b on a.sno = b.sno

drop table #tb
q191727779 2012-03-19
  • 打赏
  • 举报
回复
是求每个学生所对应的成绩的平均成绩,结果插入到表score中,你的语句执行结果是插入了重复的学生的平均成绩,而且平均成绩不对[Quote=引用 6 楼 acherat 的回复:]
1楼不行么?为什么?
[/Quote]
q191727779 2012-03-19
  • 打赏
  • 举报
回复
不行啊亲,表里没有class的,score表现在是空表,列名只有sno,sname,avg_score,都是要帅选,算出结果才插入到score表中啊!!![Quote=引用 3 楼 usera_dmin 的回复:]
SQL code

INSERT INTO SCORE(CLASS,SName,SCORE)
SELECT S.CLASS,S.SName,AVG(C.SCORE) FROM Student S JOIN SC C
ON S.SNO=C.SNO
--WHERE
GROUP BY S.SName,S.CLASS
[/Quote]
AcHerat 元老 2012-03-19
  • 打赏
  • 举报
回复
1楼不行么?为什么?
wfkmu 2012-03-19
  • 打赏
  • 举报
回复
你可以自己修改一下嘛,正好可以锻炼
q191727779 2012-03-19
  • 打赏
  • 举报
回复
亲,是插入sno,sname,AVG_SCORE啊,没有class啊[Quote=引用 3 楼 usera_dmin 的回复:]
SQL code

INSERT INTO SCORE(CLASS,SName,SCORE)
SELECT S.CLASS,S.SName,AVG(C.SCORE) FROM Student S JOIN SC C
ON S.SNO=C.SNO
--WHERE
GROUP BY S.SName,S.CLASS
[/Quote]
UserA_dmin 2012-03-19
  • 打赏
  • 举报
回复

INSERT INTO SCORE(CLASS,SName,SCORE)
SELECT S.CLASS,S.SName,AVG(C.SCORE) FROM Student S JOIN SC C
ON S.SNO=C.SNO
--WHERE
GROUP BY S.SName,S.CLASS

q191727779 2012-03-19
  • 打赏
  • 举报
回复
不行啊,亲~~~[Quote=引用 1 楼 acherat 的回复:]
SQL code

insert into score(sno,sname,score)
select a.sno,a.sname,avg(b.score)
from student a join SC b on a.sno = b.sno
--where ...
group by a.sno,a.sname
[/Quote]
AcHerat 元老 2012-03-19
  • 打赏
  • 举报
回复

insert into score(sno,sname,score)
select a.sno,a.sname,avg(b.score)
from student a join SC b on a.sno = b.sno
--where ...
group by a.sno,a.sname
五更琉璃 2012-03-19
  • 打赏
  • 举报
回复

with t as
(select avg(score) as score,sno from sc group by sno)
insert into score(sno,sname,score)
select student.sno,student.sname,t.score from t join on Student on t.sno=Student.sno

10楼的效率太低,正式给你个

34,870

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧