第三章SQL编程--经典案例和上级题

//if-lse经典案例

--统计并显示2013-08-09 的oop考试平均分
--如果平均分在70以上,显示“考试成绩优秀”,并显示前三名学生的考试信息
--如果在70分以下,显示“考试成绩较差”,并显示后三名学生的考试信息
--01.定义一个变量,保存平均分

select * from Result

--查编号
declare @subid int 
select @subid=SubjectId from Subject
where subjectname='oop'

--查询平均分
declare @avg int
select @avg=avg(Studentresult)from result
where examdate>='2013-08-09' and examdate<'2013-08-10' and SubjectId=@subid

--前三名
if(@avg>=70)
begin
print '成绩优秀'
select top 3 * from Result  where examdate>='2013-08-09' and ExamDate<'2013-08-10' and SubjectId=@subid
order by StudentResult desc
end
if(@avg<70)
begin
print '考试成绩较差'
select top 3 * from Result  where examdate>='2013-08-09' and ExamDate<'2013-08-10' and SubjectId=@subid
order by StudentResult 
end
//while经典案例

--科目名为oop的id
declare @subid int
select @subid=SubjectId from subject
where SubjectName='oop'
print @subid

--科目名为oop的最近考试时间
declare @maxdate datetime
select @maxdate=MAX(examdate) from Result
where SubjectId=@subid


--符合条件的学生信息
select * from Result
where SubjectId=@subid and ExamDate=@maxdate

--分数小于70分的人数
declare @sum int
select @sum= COUNT(StudentNo)from Result
where StudentResult<70 and SubjectId=@subid and ExamDate=@maxdate


--当分数小于70分的人数大于0,每个人加2,95分以上不加,直至每个人的分数达到平均分
while(@sum>0)
begin
--更新分数
update result set studentresult+=2
where subjectid=@subid and  examdate=@maxdate and studentresult<95
--重新给分数小于70分的人数赋值
select @sum=COUNT(Studentno) from Result
where subjectid=@subid and  examdate=@maxdate and studentresult<70
end

//第三章上机题

--上机1
declare @name nvarchar(255)
set @name='★'
declare @int int
set @int=1
while(@int<6)
begin
print @name
set @name+='★'
set @int+=1
end
go


--上机2
select studentno as '学号',StudentName as '姓名',FLOOR(DATEDIFF(dy,birthday,getdate())/365) as '年龄' from dbo.Student where studentno='23225'
declare @yy int
select @yy=DATEPART(YY,birthday) from student where studentno='23225'
select * from student where DATEPART(YY,birthday)=@yy+1 or DATEPART(YY,birthday)=@yy-1
go


--上机3
select top 1 studentname,studentresult from student,result,subject where student.studentno='23219' and subject.subjectid=2
declare @result int
select @result=studentresult from student,result,subject where student.studentno='23219' and subject.subjectid=2
if(@result>85)
print '优秀'
else if(@result>70)
print '良好'
else if(@result>60)
print '中等'
else
print '差'

--上机4

--科目名为oop的id
declare @subid int
select @subid=SubjectId from subject
where SubjectName='oop'
print @subid

--科目名为oop的最近考试时间
declare @maxdate datetime
select @maxdate=MAX(examdate) from Result
where SubjectId=@subid

--根据课程oop最近一次考试,显示每个学生的等级
select 学号=StudentNo,课程编号=SubjectId, 成绩=StudentResult, 等级=case
when StudentResult>90 then '★★★★'
when StudentResult between 80 and 89 then '★★★'
when StudentResult between 70 and 79 then '★★'
when StudentResult between 60 and 69 then '★'
else '你要努力了'
end
from Result 
where subjectid=@subid and  examdate=@maxdate



--分数小于70分的人数
declare @sum int
select @sum= COUNT(StudentNo)from Result
where StudentResult<70 and SubjectId=@subid and ExamDate=@maxdate


--当分数小于70分的人数大于0,每个人加2,直至每个人的分数达到平均分,若成绩大于100分,则按100分计算
while(@sum>0)
begin
--更新分数
update result set studentresult+=2
where subjectid=@subid and  examdate=@maxdate 
update result set studentresult=100
where subjectid=@subid and  examdate=@maxdate and studentresult>100
--重新给分数小于70分的人数赋值
select @sum=COUNT(Studentno) from Result
where subjectid=@subid and  examdate=@maxdate and studentresult<70
end

//SQL面试题

--创建表
use MySchool

create table tmp
(
rp varchar(10),
shengfu nchar(1)
)

--插入数据
insert into tmp values('2005-5-9','胜')
insert into tmp values('2005-5-9','胜')
insert into tmp values('2005-5-9','负')
insert into tmp values('2005-5-9','负')
insert into tmp values('2005-5-10','胜')
insert into tmp values('2005-5-10','负')
insert into tmp values('2005-5-10','负')


select rp as 日期,
SUM(
case 
 when shengfu='胜' then 1
 else 0
 end
) as 胜,
SUM(
case 
  when shengfu='负' then 1
  else 0
  end
  ) as 负
from tmp
group by rp
order by rp desc 

//SQL语句打印直角三角形

---单层循环

declare @str nvarchar(32)
set @str='☆'

declare @num int
set @num=1

while(@num<5)
begin
print @str
set @str+='☆'
set @num+=1

end

--双层循环 


declare @number int
set @number=1
declare @sum int
set @sum=1
declare @stri nvarchar(32)
set @stri=''
while(@number<=5)
begin
while(@sum<@number)
begin
set @stri+='☆'
set @sum+=1
end
print @stri
set @number+=1
end


你可能感兴趣的:(SQL,数据库设计)