不知道怎么用一条语句实现,大家给看看,谢谢

DelphiStudy 2004-04-28 09:50:39
成绩记录
TABLE Grade(
StuSysID --学生系统编号
CourseSysID --课程系统编号
Credit --学分(整数)
Grade --成绩(在0-100之间的整数)
)
StuSysID是从学生信息里选出来的,表示需要进行计算的学生,数量不定.
CourseSysID是从其他表里选出来的,表示学生学过的课程,数量不定.
一个学生的成绩有很多条这样de记录,数目不定.
我现在要求每个学生的平均成绩,并插入另外一个表里.
平均分de算法,我给个例子说明一下:
StuSysID=345
CourseSysID IN [1,2,3],成绩分别是95 65 85,学分是8 6 6
平均分=(95*8+65*6+85*6)/(8+6+6)

学生有很多,每个人都学过很多课。够麻烦的吧,我搞不定,大家帮帮忙,谢谢。
...全文
42 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
DelphiStudy 2004-04-29
  • 打赏
  • 举报
回复
谢谢大家!我想大家的办法肯定够用啦!
结贴,给分。
shuichangliu 2004-04-29
  • 打赏
  • 举报
回复
select a.studyID,
[avgGrade]=sum(a.credit*a.grade)/sum(a.credit)
--into #tmpTable
from grade a
group by a.studyID
shuichangliu 2004-04-29
  • 打赏
  • 举报
回复
--测试数据
create table Grade
( studyID varchar(10),
courseSysID varchar(10),
credit tinyint,
grade int default 0 check(grade>=0 and grade<=100)
)
insert Grade select 'XH0001','KC001', 8,80
union all select 'XH0001','KC002', 6,60
union all select 'XH0001','KC003', 4,30
union all select 'XH0001','KC004', 5,79
union all select 'XH0001','KC005',10,99
union all select 'XH0002','KC002', 6,80
union all select 'XH0002','KC003', 4,55
union all select 'XH0002','KC004', 5,66
union all select 'XH0003','KC001', 8,88
union all select 'XH0003','KC002', 6,61
union all select 'XH0003','KC004', 5,58


----测试语句
select a.studyID,
[avgGrade]=(select top 1 sum(b.credit*b.grade)/sum(b.credit)
from grade b where b.studyID=a.studyID)
--into #tmpTable
from
(select distinct studyID from Grade) a

--测试结果
studyID avgGrade
---------- -----------
XH0001 75
XH0002 68
XH0003 71

(所影响的行数为 3 行)
shuichangliu 2004-04-29
  • 打赏
  • 举报
回复
搞得测试数据来啥!!
DelphiStudy 2004-04-28
  • 打赏
  • 举报
回复
大家帮帮忙啊
DelphiStudy 2004-04-28
  • 打赏
  • 举报
回复
将 expression 转换为数据类型 tinyint 时发生算术溢出错误。
cast(sum(Credit*Grade) as numeric(10,2))有错
cast(sum(Credit) as numeric(10,2))没错
怎么办呢?
8992026 2004-04-28
  • 打赏
  • 举报
回复
select StuSysID,cast(sum(Credit*Grade) as numeric(10,2))/sum(Credit) as avgGrade
from Grade
group by StuSysID

34,576

社区成员

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

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