34,870
社区成员




if object_id('sc')is not null drop table sc
go
Create table SC(
sno char(6) NOT NULL,
cno char(2) NOT NULL,
grade numeric(2))
INSERT into SC(sno,cno,grade)
select '200101','c1',90 union all
select '200101','c2',85 union all
select '200101','c3',80 union all
select '210101','c1',85 union all
select '210101','c2',75 union all
select '220101','c3',70 union all
select '230101','c1',99 union all
select '230101','c3',88 union all
select '230101','c2',88
--上面是csdn是我今天早上在csdn获得的绝对没问题
Select sno,AVG(grade) AS avg_grade from SC group by sno having AVG(grade) > 83 order by avg_grade DESC;
Select sno,AVG(grade) AS avg_grade from SC group by sno having avg_grade > 83 order by avg_grade DESC;
--报错:列名'avg_grade' 无效。既然可以用avg_grade来将序排列,为什么不可以avg_grade > 83呢?大家讨论下!
--因为书上(不知道N年前的数据库版本)的是用下面这行语句,而且按自然人的思想也应该是下面这行更能表达意思.
/*
这个是用在聚合函数的用法。当我们在用聚合函数的时候,一般都要用到GROUP BY 先进行分组,然后再进行聚合函数的运算。运算完后就要用到HAVING 的用法了,就是进行判断了,例如说判断聚合函数的值是否大于某一个值等等
*/