【技术支持】Oracle plsql中的having子句并非都能改写成where子句。having子句的适用场景。

来源:互联网 发布:linux expect spwan 编辑:程序博客网 时间:2024/05/22 07:02

一句话:

group by子句让聚合函数输出不同的记录,having子句优先级低于聚合函数,其就是用于筛选聚合后的各组值,以弥补where子句的不足。所以,having子句中的条件必须是聚合函数。


例子:

update t_userinfo tu set tu.status = 3         , tu.update_time = to_date(str_date, 'yyyy-mm-dd hh24:mi:ss')    where tu.status = 1      and exists (select 1 from t_annoyance ta                 where ta.call_userid = tu.userid                   and ta.insert_time >= to_date(str_date, 'yyyy-mm-dd hh24:mi:ss') - 30                   and ta.call_time < 6                  group by tu.userid                 having count(distinct ta.becall_userid) > 50)

参考:

http://zhidao.baidu.com/link?url=WOtpjrbl3LLNX08hvGrG4UKjtq5Jh1_j1tNax3s-Ygg33n5O0n9pAQAFa2Z_ZSyPNLy51raZ-3WSOfemClj41a


0 0
原创粉丝点击