对学员成绩表进行查询,查询任意两科及格的学员数量

 

对学员成绩表进行查询

要求:

1.查处任意及格两科的学员数量

2.使用存储过程

大家看看下面这种查询办法可行吗,有没有更好的方法!

数据库表如下图:

name 代表学员名称 f1 \f2\f3\'f4代表课程成绩

存储过程如下:

方法一:

create proc  tests
as
declare @agee float

set @agee=60

select count(name) from test a where  (a.f1>=@agee and a.f2>=@agee) or (a.f1>=@agee and a.f3>=@agee) or (a.f1>=@agee and a.f4>=@agee) or
(a.f2>=@agee and a.f3>=@agee) or (a.f2>=@agee and a.f4>=@agee) or (a.f3>=@agee and a.f4>=@agee)

exec tests

经过各位同仁的评论:还有两种更为简洁的方法,现在补充在下面,以方便各位的查看!

方法二:

select count(*)
from dbo.test where case when f1 >= 60 then 1 else 0 end +
case when f2 >= 60 then 1 else 0 end +
case when f3 >= 60 then 1 else 0 end +
case when f4 >= 60 then 1 else 0 end >= 2
方法三:
SELECT COUNT(*)
FROM dbo.test
WHERE f1 / 60 + f2 / 60 + f3 / 60 + f4 / 60 >= 2

 

posted @ 2012-07-23 16:34  狼人天下  阅读(993)  评论(14编辑  收藏  举报