用T-sql语句写出 查询出平均分大于80分,且至少两门课大于80的学生。 表如下:

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用T-sql语句写出 查询出平均分大于80分,且至少两门课大于80的学生。 表如下:相关的知识,希望对你有一定的参考价值。

create table student
(
id int primary key ,
name varchar(10) not null,
major varchar(10) not null,
score int ,
sex varchar(2) not null
)
go
插入如下数据:
insert into student values(1,'邱君','语文',70,'女')
insert into student values(2,'小狗','语文',76,'男')
insert into student values(3,'混蛋','语文',60,'男')

insert into student values(4,'邱君','数学',81,'女')
insert into student values(5,'混蛋','数学',90,'男')
insert into student values(6,'小狗','数学',77,'男')

insert into student values(7,'邱君','外语',98,'女')
insert into student values(8,'小狗','外语',71,'男')
insert into student values(9,'混蛋','外语',88,'男')
go
输出如下图形:
姓名 数学 外语 语文 平均成绩
邱君 81 98 76 82
下面的不对啊

参考技术A 稍微简化一下:
SELECT NAME,
SUM(CASE WHEN MAJOR = '数学' THEN SCORE ELSE 0 END)数学 ,
SUM(CASE WHEN MAJOR = '外语' THEN SCORE ELSE 0 END)外语 ,
SUM(CASE WHEN MAJOR = '语文' THEN SCORE ELSE 0 END)语文 ,
AVG(SCORE)
FROM STUDENT GROUP BY NAME
having SUM(CASE WHEN SCORE > 80 THEN 1 ELSE 0 END) >= 2
AND AVG(SCORE) >80
参考技术B SELECT id,
SUM(CASE WHEN major = '数学' THEN score ELSE 0 END) ,
SUM(CASE WHEN major = '外语' THEN score ELSE 0 END) ,
SUM(CASE WHEN major = '语文' THEN score ELSE 0 END) ,
AVG(score)
FROM student
WHERE student.id IN (SELECT id FROM student GROUP BY id HAVING SUM(CASE WHEN score > 80 THEN 1 ELSE 0 END) >= 2)
GROUP BY id HAVING AVG(score) > 80

即可追问

不对啊

追答

什么问题?

追问

SELECT name,
SUM(CASE WHEN major = '数学' THEN score ELSE 0 END)数学 ,
SUM(CASE WHEN major = '外语' THEN score ELSE 0 END)外语 ,
SUM(CASE WHEN major = '语文' THEN score ELSE 0 END)语文
FROM student
WHERE name IN (SELECT name FROM student GROUP BY name HAVING SUM(CASE WHEN score > 80 THEN 1 ELSE 0 END) >= 2)
GROUP BY name HAVING AVG(score) > 80

我把你的改了一下就好了 谢谢你的回答!

追答

恩。我也刚发现了。

你的少了一个平均成绩
SELECT name,
SUM(CASE WHEN major = '数学' THEN score ELSE 0 END)数学 ,
SUM(CASE WHEN major = '外语' THEN score ELSE 0 END)外语 ,
SUM(CASE WHEN major = '语文' THEN score ELSE 0 END)语文,
AVG(score)
FROM student
WHERE name IN (SELECT name FROM student GROUP BY name HAVING SUM(CASE WHEN score > 80 THEN 1 ELSE 0 END) >= 2)
GROUP BY name HAVING AVG(score) > 80

3楼那样要简化点,不过貌似having加多条件过滤后反而慢

SELECT name,
SUM(CASE WHEN major = '数学' THEN score ELSE 0 END)数学 ,
SUM(CASE WHEN major = '外语' THEN score ELSE 0 END)外语 ,
SUM(CASE WHEN major = '语文' THEN score ELSE 0 END)语文,
AVG(score)
FROM student
GROUP BY name
HAVING AVG(score) > 80 AND SUM(CASE WHEN score > 80 THEN 1 ELSE 0 END) >= 2

本回答被提问者和网友采纳
参考技术C 对的吧

仅显示子查询中的 1 列:列出平均价格 >= 3 且至少有 2 个不同产品的麸皮名称

【中文标题】仅显示子查询中的 1 列:列出平均价格 >= 3 且至少有 2 个不同产品的麸皮名称【英文标题】:display only 1 column from the subquery : list the bran_name whos avg price is >= 3 and also have at least 2 distinct products 【发布时间】:2021-03-21 07:06:18 【问题描述】:

这是我的查询:

select avg(p2.price), p2.brand_name, count(distinct p2.product_id) 
count_of_products
from product p2
group by p2.brand_name
having count(distinct p2.product_id) >= 2 and avg(p2.price) > 3

但我只需要在最终查询中显示band_name。 我试过这个:

select p1.brand_name from product p1,
(select avg(p2.price), p2.brand_name, count(distinct p2.product_id) 
count_of_products
from product p2
group by p2.brand_name
having count(distinct p2.product_id) >= 2 ) p2 and and avg(p2.price) > 3
where 
p1.brand_name = p2.brand_name

但它给了我以下错误:

>[Error] Script lines: 3-9 --------------------------
 No column name was specified for column 1 of 'p2'.

【问题讨论】:

【参考方案1】:

但我只需要在最终查询中显示 band_name

所以只需从select 子句中删除其他列:

select brand_name
from product
group by brand_name
having count(distinct product_id) >= 2 and avg(price) > 3

【讨论】:

更新了我的问题:我只需要那些平均(价格)大于 3 @GMB 的品牌 @gabs:您可以将该条件添加到having 子句中。

以上是关于用T-sql语句写出 查询出平均分大于80分,且至少两门课大于80的学生。 表如下:的主要内容,如果未能解决你的问题,请参考以下文章

统计每门课程的选课人数和平均分,结果只显示平均分大于等于80的记录(使用T-SQL语句进行单表查询:)

SQL查询出平均分大于80分,且至少两门课大于90的学生。

用SQL语句写出完成如下查询

查询平均成绩大于80分的课程的名字和学分.SQL语句查询,跪求高人指点!我积分不多,这次都悬赏出去了

数据库有张表,里面有字段:姓名、语文、数学、英语,怎么显示每个学生的平均成绩

SQL语句查询每个学生的学号、姓名、平均成绩、最高成绩和最低成绩