對學員成績表進行查詢,查詢任意兩科及格的學員數量


 

對學員成績表進行查詢

要求:

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM