聚合函数不能在where子句使用

1、查询出销售表中,销售额大于本地区平均水平的记录,用一条SQL语句

--创建sales表

create table sales( OrderID int,Region char(1),Total float )

--向表中插入数据

insert into sales

select '2','C',80.00 union

select '3','A',130.00 union

select '4','B',90.00 union

select '6',120.00 union

select '7',90.00 union

select '9',80.00 union

select '1',100.00 union

select '5',100.00 union

select '8',90.00

--容易出现的错误例子:

select *

from sales

where total>sum(total)/count(1)

group by region

--正确的查询:

select a.* from sales a,(select region,sum(total)/count(1) as av from sales group by region) b

where a.region=b.region and a.total>b.av

相关文章

什么是设计模式一套被反复使用、多数人知晓的、经过分类编目...
单一职责原则定义(Single Responsibility Principle,SRP)...
动态代理和CGLib代理分不清吗,看看这篇文章,写的非常好,强...
适配器模式将一个类的接口转换成客户期望的另一个接口,使得...
策略模式定义了一系列算法族,并封装在类中,它们之间可以互...
设计模式讲的是如何编写可扩展、可维护、可读的高质量代码,...