您的位置:首頁技術文章
文章詳情頁

python實現Oracle查詢分組的方法示例

瀏覽:6日期:2022-07-26 18:02:57

本文實例講述了python實現Oracle查詢分組的方法。分享給大家供大家參考,具體如下:

1.分組的概念:

關鍵字:group by子句

結論:在select列表中如果出現了聚合函數,不是聚合函數的列,必須都要定義到group by子句的后面

需求:

查詢公司各個部門的平均工資?

select department_id,avg(salary)from employeesgroup by department_id;

需求提升:

查詢公司各個部門不同工種的平均工資?

select department_id,job_id,avg(salary)from employeesgroup by department_id,job_id;

2.having子句:

作用:用來過濾包含聚合函數的相關信息(數據)

位置:

可以再group by前也可以再 group by后面(比較隨意)

需求:

查詢40、60、80號部門中平均工資大于6000的部門信息?

以下代碼實現有問題的:報錯了??!

報錯原因:如果需要對于聚合函數進行過濾不能使用where子句,

需要使用having子句來實現...

select department_id,avg(salary)from employeeswhere avg(salary) > 6000 and department_id in(40,60,80)group by department_id;

代碼修改如下:

select department_id,avg(salary)from employeeswhere department_id in(40,60,80)having avg(salary) > 6000group by department_idorder by department_id desc;

附:Python連接與查詢oracle數據庫示例:

import cx_Oracleconn = 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()

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python常見數據庫操作技巧匯總》、《Python編碼操作技巧總結》、《Python數據結構與算法教程》、《Python Socket編程技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對大家Python程序設計有所幫助。

標簽: Python 編程
相關文章:
国产综合久久一区二区三区