Oracle函數組的使用

來源:互聯網
上載者:User

標籤:

--1.組函數
--COUNT():用來統計記錄的條數 如果沒有記錄,返回 0
--COUNT函數可以根據一列或多列進行計算,沒有排重功能
--統計EMP表一共有多少條記錄
select count(empno) from emp;
select count(*) from emp;

--統計EMP中一共有多少種工作
select count(distinct job) from emp;

--MAX()計算最大值
--查出EMP表中最高工資
select max(sal) from emp;

--MIN()計處最小值
--查出EMP表中的最低工資
select min(sal) from emp;

--SUM()計算總和
--查詢EMP表中的員工工資總和
select sum(sal) from emp;

--5) AVG()計算平均值 --> 空值的話會忽略,不會計算在內
--查詢員工平均工資
select round(avg (sal)) from emp;

--------------------------------------------------------------------------------
--2.分組統計
--查詢出各部門的員工數目
select deptno,count(*) from emp group by deptno;

--求出各部門平均工資
select deptno,round(avg(sal)) from emp group by deptno;

--按照職位分組,求出每個職位的最高和最低工資
select job,max(sal) 最高工資,min(sal) 最低工資 from emp group by job;

--按照職位分組,統計平均工資最高的工資
SELECT MAX(AVG(sal)) FROM emp GROUP BY job;

--按部門名稱分組,顯示部門名稱,以及每個部門的員工數
select d.dname,count(e.empno) from emp e, dept d where e.deptno = d.deptno group by d.dname;

--查詢出每個部門的名稱,部門的人數,平均工資
select d.dname,count(e.empno) 人數,round(nvl(avg(e.sal),0)) 平均工資 from emp e, dept d where e.deptno = d.deptno group by d.dname;

--一般都是按照一個欄位來分組,如果按照多個欄位來分組,那麼必須多個欄位都一樣才算一個組
--查詢出每個部門的編號,名稱,位置,部門的人數,平均工資
select d.deptno,d.dname,d.loc,count(e.empno),round(nvl(avg(e.sal),0)) 平均工資
from dept d,emp e where e.deptno = d.deptno(+) group by d.deptno, d.dname,d.loc;

--查出平均工資大於2000的部門編號與平均工資
select deptno,round(nvl(avg(sal),0)) from emp group by deptno having round(nvl(avg(sal),0)) > 2000;

--顯示非銷售人員工作名稱以及從事同一工作的僱員的月工資總和,並且滿足從事同一工作的員工的月工資合計大於5000,輸出的結果按月工資總和排序
select job,round(sum(sal)) 工資和 from emp where job != ‘SALESMAN‘ group by job having round(sum(sal)) > 5000 order by round(sum(sal)) desc;

--查詢出平均工資最高的部門
select max(round(avg(sal))) from emp group by deptno;

Oracle函數組的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.