當前位置:首頁 » 編程語言 » sql用case查詢成績等級
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql用case查詢成績等級

發布時間: 2023-08-27 01:01:27

A. 資料庫中查詢課程號的成績並將成績分為等級制怎麼寫

這個可以通過case when 語句實現。
大致是這樣
select 課程號,成績,case when 成績>=90 then 'A' when 成績<90 and 成績>=80 then 'B' else 'C' end as 等級 from 表名。
請參考,如果有幫助到你,請點擊採納。

B. sql通過分數查詢所在等級 急急急!!!

select 員工編號,考核分數,等級=case
when 考核分數<=100 and 考核分數>=90 then '優等'
when 考核分數<=89 and 考核分數>=80 then '甲等'
when 考核分數<=79 and 考核分數>=70 then '乙等'
when 考核分數<=69 and 考核分數>=60 then '丙等'
else '丁等' end
from employee_Test

C. 查詢每個學生的各科成績sql語句

1、查詢每個學生的各科成績sql語句:

select a.studentid,a.name,a.sex,v1.score as '語文',v2.score as '數學', v3.score as '英語',v4.score

as 『哲學』, (v1.score+v2.score+v3.score+v4.score)/4 as 『平均成績』 from Stuednt a
left join

(select studentid,score from grade where cid=(select cid from course where cname='語文'))as v1

on a.studentid=v1.studentid

left join

(select studentid,score from grade where cid=(select cid from course where cname='數學'))as v2

on a.studentid=v2.studentid

left join

(select studentid,score from grade where cid=(select cid from course where cname='英語'))as v3

on a.studentid=v3.studentid

left join

(select studentid,score from grade where cid=(select cid from course where cname='哲學'))as v4

on a.studentid=v4.studentid

order by a.studentid

2、sql資料庫介紹:

(1)SQL是Structured Query Language(結構化查詢語言)的縮寫。SQL是專為資料庫而建立的操作命令集,是一種功能齊全的資料庫語言。在使用它時,只需要發出"做什麼"的命令,"怎麼做"是不用使用者考慮的。

(2)SQL功能強大、簡單易學、使用方便,已經成為了資料庫操作的基礎,並且現在幾乎所有的資料庫均支持SQL。

(3)SQL資料庫的數據體系結構基本上是三級結構,但使用術語與傳統關系模型術語不同。

(4)在SQL中,關系模式(模式)稱為"基本表"(base table);存儲模式(內模式)稱為"存儲文件"(stored file);子模式(外模式)稱為"視圖"(view);元組稱為"行"(row);屬性稱為"列"(column)。

D. 怎樣使用sql語句可以多條件查詢 比如成績等級劃分

select sum(case when '成績'=100.0 then 1 else 0 end),
sum(case when '成績'<100 and '成績' >=90 then 1 else 0 end),
sum(case when '成績'<90 and '成績' >=80 then 1 else 0 end),
sum(case when '成績'<80.0 and '成績' >=70 then 1 else 0 end),
sum(case when '成績'<70.0 and '成績' >=60 then 1 else 0 end),
sum(case when '成績'<60 then 1 else 0 end) from table
可以按分數間隔統計出成績分布,分別是100分有多少人,90~100,80~90,70~80,60~70,60以下的區間分別有多少人。

E. SQL如何根據分數進行分組

select成績,
casewhen成績>=90then'A'when成績>=80then'B'when成績>=70then'C'else'D'endas`group`
from表;

Case具有兩種格式。簡單Case函數和Case搜索函數。

--簡單Case函數

CASEsexWHEN'1'THEN'男'WHEN'2'THEN'女'ELSE'其他'END

--Case搜索函數

CASEWHENsex='1'THEN'男'WHENsex='2'THEN'女'ELSE'其他'END

該例中即可採用case搜索函數

F. 1查詢成績表的總分數,平均分,最低分和最高分。用sql語句怎麼寫

---1. 計算每個人的總成績並排名(要求顯示欄位:姓名,總成績)
select name,sum(cast(score as bigint)) as allscore from stuscore group by name order by allscore desc
---2. 計算每個人的總成績並排名(要求顯示欄位: 學號,姓名,總成績)
select stuid,name,sum(cast(score as bigint)) as allscore from stuscore group by stuid,name order by allscore desc
---3. 計算每個人單科的最高成績(要求顯示欄位: 學號,姓名,課程,最高成績)
SELECT t1.stuid,t1.name,t1.subject,t1.score from stuscore t1,(SELECT stuid,max(score) as maxscore from stuscore group by stuid) t2 where t1.stuid=t2.stuid and t1.score=t2.maxscore
---4. 計算每個人的平均成績(要求顯示欄位: 學號,姓名,平均成績)
select distinct t1.stuid,t1.name,t2.avgscore from stuscore t1,(select stuid,avg(cast(score as bigint)) as avgscore from stuscore group by stuid) t2 where t1.stuid=t2.stuid
---5. 列出各門課程成績最好的學生(要求顯示欄位: 學號,姓名,科目,成績)
select t1.stuid,t1.name,t1.subject,t2.maxscore from stuscore t1,(select subject,max(score) as maxscore from stuscore group by subject) t2 where t1.subject=t2.subject and t1.score=t2.maxscore
---6. 列出各門課程成績最好的兩位學生(要求顯示欄位: 學號,姓名,科目,成績)
select distinct t1.* from stuscore t1 where t1.stuid in(select top 2 stuscore.stuid from stuscore where subject = t1.subject order by score desc)order by t1.subject
---7. 統計報表(要求顯示欄位: 學號,姓名,各科成績,總分,平均成績)
select stuid as 學號,name as 姓名,sum(case when subject='語文' then score else 0 end) as 語文,sum(case when subject='數學' then score else 0 end) as 數學,sum(case when subject='英語' then score else 0 end) as 英語,sum(cast(score as bigint)) as 總分,(sum(cast(score as bigint))/count(*)) as 平均分 from stuscore group by stuid,name order by 總分 desc
---8. 列出各門課程的平均成績(要求顯示欄位:課程,平均成績)
select subject,avg(cast(score as bigint)) as avgscore from stuscore group by subject
---9. 列出數學成績的排名(要求顯示欄位:學號,姓名,成績,排名)
select * from stuscore where subject ='數學' order by score desc
---10. 列出數學成績在2-3名的學生(要求顯示欄位:學號,姓名,科目,成績)
select t3.* from(select top 2 t2.* from (select top 3 name,subject,score,stuid from stuscore where subject='數學' order by score desc) t2 order by t2.score) t3 order by t3.score desc
---11. 求出李四的數學成績的排名
declare @tmp table(pm int,name varchar(50),score int,stuid int)
insert into @tmp select null,name,score,stuid from stuscore where subject='數學' order by score desc
declare @id int
set @id=0;
update @tmp set @id=@id+1,pm=@id
select * from @tmp where name='李四'
---12. 統計各科目及格人數
select subject,
(select count(*) from stuscore where score<60 and subject=t1.subject) as 不及格,
(select count(*) from stuscore where score between 60 and 80 and subject=t1.subject) as 良,
(select count(*) from stuscore where score >80 and subject=t1.subject) as 優
from stuscore t1 group by subject
---13.統計如下:數學:張三(50分),李四(90分),王五(90分),趙六(76分)
declare @s varchar(1000)
set @s=''
select @s =@s+','+name+'('+convert(varchar(10),score)+'分)' from stuscore where subject='數學'
set @s=stuff(@s,1,1,'')
print '數學:'+@s

G. sql把student表中大於85分的顯示為優,75-84為良,60-74及格,小於60顯示為不及格,空的顯示為缺考

select name,chengji,(case when chengji >85 then '優'
when chengji between 75 and 84 then '良'
when chengji between 60 and 74 then '及格'
when chengji <60 then '不及格'
when chengji is null then '缺考' end) 分數等級評價
from student;
運行結果:
name 分數 分數等級評價
小紅 55 不及格
王一 89 優

H. 用SQL語句顯示班級學號成績並算出優良中差

select 班級,學號,(case when 成績擾答>90 then '優'
when 成績>銀中80 and 成鋒李山績<=90 then '良'
when 成績>60 and 成績<=80 then '中'
else '差' end) as 成績
from table_name

I. 關於SQL中case…when…then語句有到題目不會,求解!!!

參照如下寫法即可:
SELECT CASE
WHEN 成績 >= 90 THEN
'優秀'
WHEN 成績 >= 80 AND 成績 < 90 THEN
'良好'
WHEN 成績 >= 70 AND 成績 < 80 THEN
'中等'
WHEN 成績 >= 60 AND 成績 < 70 THEN
'及格'
ELSE
'不及格'
END
FROM 表名
WHERE 條件...;