having列,不是group by的列,也不是聚合函数
测试内容:having用法测试,写在where后面的条件加在having后面去
结果:clickhouse上面可以执行,oracle运行报错不是GROUP BY 表达式
PS:数据库系统工程师的题库里面有这个用法,因颠覆了我的认知,所以测试下
第1次测试
SELECT "版本",COUNT(1),MAX("企业板块") from tableA
WHERE "企业板块"='金融板块'
group by "版本"
确实等价于
SELECT "版本",COUNT(1),MAX("企业板块") from tableB
group by "版本"
having "企业板块"='金融板块'
不过过滤写在where后面性能更加好
第2次测试
DROP table cs_thz_2021102801;
create table cs_thz_2021102801(
empno String,
empname String,
deptname String,
zhiwei String,
sal Float32
)
ENGINE=Log
;
SELECT * FROM cs_thz_2021102801
;
INSERT into cs_thz_2021102801(empno,empname,deptname,zhiwei,sal)
values('00001','张三','开发部','职员',200);
INSERT into cs_thz_2021102801(empno,empname,deptname,zhiwei,sal)
values('00002','张三2','开发部','经理',600);
INSERT into cs_thz_2021102801(empno,empname,deptname,zhiwei,sal)
values('00003','张三3','开发部','职员',400);
INSERT into cs_thz_2021102801(empno,empname,deptname,zhiwei,sal)
values('00004','张三4','开发部','经理',800);
INSERT into cs_thz_2021102801(empno,empname,deptname,zhiwei,sal)
values('00005','张三5','销售部','职员',100);
INSERT into cs_thz_2021102801(empno,empname,deptname,zhiwei,sal)
values('00006','张三6','销售部','经理',200);
INSERT into cs_thz_2021102801(empno,empname,deptname,zhiwei,sal)
values('00007','张三7','销售部','职员',300);
INSERT into cs_thz_2021102801(empno,empname,deptname,zhiwei,sal)
values('00008','张三8','销售部','经理',400);
SELECT deptname,avg(sal) FROM cs_thz_2021102801
group by deptname
having zhiwei='经理'
;