SQL考试题目.请大家帮帮忙啊~~!

angel32697 2010-08-20 11:24:51
CREATE DATABASE DB2

CREATE TABLE Student
(
StudentID INT NOT NULL PRIMARY KEY, -- 学号
StudentName NVARCHAR(30) NOT NULL, -- 姓名
Sex BIT NOT NULL DEFAULT(0), -- 性别
[Address] NVARCHAR(100) NOT NULL --地址
)



CREATE TABLE ScoreMain
(
ExamID INT NOT NULL PRIMARY KEY, -- 考试号
StudentID INT NOT NULL REFERENCES Student(StudentID), --学生编号
ExamMonth DATETIME NOT NULL, --考试日期
Teacher NVARCHAR(30) NOT NULL, -- 负责老师
MemoText NVARCHAR(100) NOT NULL -- 备注说明
)



CREATE TABLE ScoreDetail
(
IndexID INT NOT NULL PRIMARY KEY, -- 流水号
ExamID INT NOT NULL REFERENCES ScoreMain(ExamID) , -- 考试号
SubjectID INT NOT NULL, -- 科目号
Score DECIMAL(18,2) NOT NULL -- 得分

)

-- 其中: SubjectID字段中仅有三种科目:1 代表语文, 2 代表数学, 3 代表英语


-- 1. 写一SQL语句,返回学生每月的各科成绩 返回格式 --学生号 姓名 考试月份 语文 数学 英语

-- 2. 写一SQL语句,返回学生每月的各科总分。返回格式--学生号 姓名 考试月份 总分

-- 3. 写一SQL语句,把学生姓名中含有的空格去掉,并将学生姓名中所有姓"章"的同学改成姓"张"
-- 4. 写一函数,返回所有学生某一年的各科平均分。
--学生号 姓名 语文平均分 数学平均分 英语平均分
-- 5. 写一个标量函数,根据得分返回相应的文本信息。
-->= 90 优秀
-->= 80 优良
-->= 60 及格
--<60 不及格
--然后写一个存储过程,返回学生成绩,同时增加一列调用该函数,返回成绩文本信息。
...全文
316 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jingjingzhanghy 2010-12-16
  • 打赏
  • 举报
回复
hello926 2010-08-26
  • 打赏
  • 举报
回复
1.
with t
as
(
select a.studentid,a.studentname,b.ExamMonth,b.examid
from student a inner join scoremain b
on a.studentid=b.studentid)
select t.studentid,t.studentname,t.ExamMonth,
sum(case c.subjectid when 1 then c.score end ) as '英语',
sum(case c.subjectid when 2 then c.score end ) as '语文',
sum(case c.subjectid when 3 then c.score end ) as '数学'
from t
inner join scoredetail c
on t.examid=c.examid
where t.exammonth=c.exammonth
group by t.studentid,t.studentname,t.exammonth,t.examid;


2.
with t
as
(
select a.studentid,a.studentname,b.ExamMonth,b.examid
from student a inner join scoremain b
on a.studentid=b.studentid)
select t.studentid,t.studentname,t.exammonth,
sum(c.score) as '总分'
from t inner join scoredetail c
on t.examid=c.examid
where t.exammonth=c.exammonth
group by t.studentid,t.studentname,t.exammonth;

3.
select replace(replace(studentname, '章', '张'),' ','') from student;

4.
CREATE function [dbo].[f](@date datetime)
returns table
as
return select t.studentid,t.studentname,
avg(case c.subjectid when 1 then c.score end ) as '英语',
avg(case c.subjectid when 2 then c.score end ) as '语文',
avg(case c.subjectid when 3 then c.score end ) as '数学'
from (select a.studentid,a.studentname,b.exammonth,b.examid
from student a inner join scoremain b
on a.studentid=b.studentid) t
inner join scoredetail c
on t.exammonth=c.exammonth
where year(c.exammonth)=year(@date) and t.examid=c.examid
group by t.studentid,t.studentname

5.
create function fun(@姓名 nvarchar(30),@考试时间 datetime,@考试课目 int)
returns varchar(10)
begin
declare @成绩 varchar(10)
select @成绩= case when c.score >=90 then '优秀'
when c.score between 80 and 90 then '优良'
when c.score between 60 and 80 then '及格'
else '不及格'
end
from scoredetail c inner join
(select a.studentid,a.studentname,b.ExamMonth,b.examid
from student a inner join scoremain b on a.studentid=b.studentid) t
on t.examid=c.examid
where t.exammonth=c.exammonth and
t.studentname=@姓名 and
t.exammonth=@考试时间 and
c.subjectid=@考试课目
return @成绩
end
go
print '小李的英语成绩为:' + dbo.fun('小李','2009/07/01',2)

我觉得应该给scoredetail增加一列考试日期。
shuchong_123 2010-08-25
  • 打赏
  • 举报
回复
都是比较基础的,建议多看看资料,多想想,相信自己可以搞定的。呵呵……
叶子 2010-08-21
  • 打赏
  • 举报
回复
已给出表结构,再给出表数据就好了
hao1hao2hao3 2010-08-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sqlcenter 的回复:]
自己写吧,会多少写多少。
[/Quote]

嗯!支持!



SQLCenter 2010-08-21
  • 打赏
  • 举报
回复
自己写吧,会多少写多少。
sheshuiping 2010-08-21
  • 打赏
  • 举报
回复
基础部分,建议自己多想想练练.
tclwlwzz 2010-08-21
  • 打赏
  • 举报
回复
这些都很基础
第一题,考察表连接
第二题,考察聚合函数
第三题,考察rtrim,ltrim,replace函数
第四题,year函数,聚合函数
第五题,over子句

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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