最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
python如何实现Oracle查询分组 python实现Oracle查询分组方法
时间:2020-04-30 编辑:袖梨 来源:一聚教程网
python如何实现Oracle查询分组?本篇文章小编给大家分享一下python实现Oracle查询分组方法,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
1.分组的概念:
关键字:group by子句
结论:在select列表中如果出现了聚合函数,不是聚合函数的列,必须都要定义到group by子句的后面
需求:
查询公司各个部门的平均工资?
select department_id,avg(salary) from employees group by department_id;
需求提升:
查询公司各个部门不同工种的平均工资?
select department_id,job_id,avg(salary) from employees group by department_id,job_id;
2.having子句:
作用:用来过滤包含聚合函数的相关信息(数据)
位置:
可以再group by前也可以再 group by后面(比较随意)
需求:
查询40、60、80号部门中平均工资大于6000的部门信息?
以下代码实现有问题的:报错了!!
报错原因:如果需要对于聚合函数进行过滤不能使用where子句,
需要使用having子句来实现...
select department_id,avg(salary) from employees where avg(salary) > 6000 and department_id in(40,60,80) group by department_id;
代码修改如下:
select department_id,avg(salary) from employees where department_id in(40,60,80) having avg(salary) > 6000 group by department_id order by department_id desc;
附:Python连接与查询oracle数据库示例:
import cx_Oracle conn = cx_Oracle.connect('scott/tiger@localhost:1521/orcl') cursor = conn.cursor() cursor.execute("SELECT ENAME FROM EMP") row = cursor.fetchone() print row[0], cursor.close() conn.close()
相关文章
- Golang ProtoBuf的基本语法详解 10-20
- Python识别MySQL中的冗余索引解析 10-20
- Python+Pygame绘制小球代码展示 10-18
- Python中的数据精度问题介绍 10-18
- Python随机值生成的常用方法介绍 10-18
- python3解压缩.gz文件分析 09-27