對學員成績表進行查詢
要求:
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 f1 / 60 + f2 / 60 + f3 / 60 + f4 / 60 >= 2
FROM dbo.test
WHERE f1 / 60 + f2 / 60 + f3 / 60 + f4 / 60 >= 2

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