加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-13 20:30:28 所属栏目:百科 来源:网络整理
导读: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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读