Oracle基礎教程:聚集、分組、行轉列

來源:互聯網
上載者:User

多行函數 聚集合函式
執行順序:
tName--where--group by --having--order by(select)

where中不能出現當前子句中的別名,也不能用聚集(分組)函數

聚集合函式嵌套的時候,不能得到單個的列

常用聚集合函式
 是對一組或一批資料進行綜合操作後返回一個結果
 count 行總數--處理空值,空值也算進去了
  count(distinct column)
  count(all column) all是預設參數,可以不寫
 avg 平均數--不處理空值
 sum 列值的和--不處理空值
 max 最大值
 min 最小值

count([{distinct|all} '列名'|*) 為列值時空不在統計之內
  為*時包含空行和重複行
idle> select count(comm) from emp;

COUNT(COMM)
-----------
   4

idle> select count(ename) from emp;

COUNT(ENAME)
------------
   14

idle> select count(*) from emp;

  COUNT(*)
----------
 14

idle>

 
idle> select count(deptno) from emp;

COUNT(DEPTNO)
-------------
    14

idle> select count(distinct deptno) from emp;

COUNT(DISTINCTDEPTNO)
---------------------
      3

idle> select count(all deptno) from emp;

COUNT(ALLDEPTNO)
----------------
       14

idle>

 


idle> select avg(sal),sum(sal),max(sal),min(sal),count(sal) from emp;

  AVG(SAL)   SUM(SAL) MAX(SAL)   MIN(SAL) COUNT(SAL)
---------- ---------- ---------- ---------- ----------
2073.21429 29025     5000 800     14

idle>


上面執行的聚集合函式都是對所有記錄統計
如果想分組統計(比如統計部門的平均值)需要使用group by 為了限制分組統計的結果需要使用having過濾
GROUP BY 分組統計  9I要排序 10G不排序


相同部門相同職位的平均工資
select deptno,job,avg(sal) from emp group by deptno,job;

求出每個部門的平均工資

idle> select deptno,avg(sal) from emp group by deptno;

    DEPTNO   AVG(SAL)
---------- ----------
 30 1566.66667
 20  2175
 10 2916.66667

idle>
分組再排序
idle> select deptno,avg(sal) from emp group by deptno order by deptno ;

    DEPTNO   AVG(SAL)
---------- ----------
 10 2916.66667
 20  2175
 30 1566.66667

idle>
分組修飾列可以是未選擇的列
idle> select avg(sal) from emp group by deptno order by deptno ;

  AVG(SAL)
----------
2916.66667
      2175
1566.66667

idle>

上面執行的分組函數都是對所有記錄統計,如果想分組統計(比如統計部門的平均值)需要使用group by 為了限制分組統計的結果需要使用having過濾
GROUP BY 分組統計  9I要排序 10G不排序

求出沒個部門的平均工資

idle> select deptno,avg(sal) from emp group by deptno;

    DEPTNO   AVG(SAL)
---------- ----------
 30 1566.66667
 20  2175
 10 2916.66667

idle>
分組再排序
idle> select deptno,avg(sal) from emp group by deptno order by deptno ;

    DEPTNO   AVG(SAL)
---------- ----------
 10 2916.66667
 20  2175
 30 1566.66667

idle>
分組修飾列可以是未選擇的列
idle> select avg(sal) from emp group by deptno order by deptno ;

  AVG(SAL)
----------
2916.66667
      2175
1566.66667

idle>

如果在查詢中使用了分組函數,任何不在分組函數中的列或運算式必須在group by子句中
因為分組函數是返回一行 而其他列顯示多行 顯示結果矛盾.
idle> select avg(sal) from emp ;

  AVG(SAL)
----------
2073.21429

idle> select deptno,avg(sal) from emp;
select deptno,avg(sal) from emp
       *
ERROR at line 1:
ORA-00937: not a single-group group function


idle> select deptno,avg(sal) from emp group by deptno ;

    DEPTNO   AVG(SAL)
---------- ----------
 30 1566.66667
 20  2175
 10 2916.66667

idle> select deptno,avg(sal) from emp group by deptno order by job;
select deptno,avg(sal) from emp group by deptno order by job
                                                         *
ERROR at line 1:
ORA-00979: not a GROUP BY expression


idle>

group by多條件分組
SCOTT@ora10g> select deptno,job,avg(sal),max(sal) from emp group by deptno,job order by 1;

    DEPTNO JOB        AVG(SAL)   MAX(SAL)
---------- --------- ---------- ----------
 10 CLERK    1300       1300
 10 MANAGER    2450       2450
 10 PRESIDENT    5000       5000
 20 ANALYST    3000       3000
 20 CLERK     950       1100
 20 MANAGER    2975       2975
 30 CLERK     950        950
 30 MANAGER    2850       2850
 30 SALESMAN    1400       1600

9 rows selected.

SCOTT@ora10g>

  • 1
  • 2
  • 下一頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.