聚合函数不能在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','C',120.00 union

select '7','A',90.00 union

select '9','B',80.00 union

select '1','A',100.00 union

select '5','B',100.00 union

select '8','C',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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值