虎虎漫画小说

繁体版 简体版
虎虎漫画小说 > > SQL语言艺术最新章节 > 第 15 章

第 15 章 免费阅读

有些过滤条件生效太晚,应该提前,可做如下修改:

select custcomr_id

from orders

where order_date < add_months(sysdate, -1)

group by custcomr_id

having sum(amount) > 0

在这个查询中,以下having 的条件乍看起来相当合理:

having sum(amount) > 0

然而,如果amount 只能是正数或零,这种having 用法就不合理。最好改为:

where amount > 0

此例中,group by的使用分两种情况。首先:

select custcomr_id

from orders

where order_date < add_months(sysdate, -1)

and amount > 0

group by custcomr_id

我们注意到,group by对聚合计算是不必要的,可以用distinct 取代它,并执行相同的排序和消

除重复项目的工作:

select distinct custcomr_id

from orders

where order_date < add_months(sysdate, -1)

and amount > 0

把条件放在wher e 子句中,能让多余的记录尽早被过滤掉,因而更高效。

总结:聚合cāo作的数据应尽量少。

-------------------------------------------------------

访问小说分享者(剁椒鱼头)的书库,阅读更多TA分享的书籍!

地址:http://www.16sy.com/u?id=14

也可以百度搜索松语文学或者访问www.16sy.com

-------------------------------------------------------

松语文学免费小说阅读_www.16sy.com

『加入书签,方便阅读』