当前位置:首页 » 编程语言 » 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 条件...;