搜尋
首頁資料庫mysql教程Oracle基础教程:聚集、分组、行转列

多行函数 聚集函数执行顺序:tName--where--group by --having--order by(select)where中不能出现当前子句中的别名,也不能用聚集

多行函数 聚集函数
执行顺序:
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>

linux

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何向新的MySQL用戶授予權限如何向新的MySQL用戶授予權限May 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

如何在MySQL中添加用戶:逐步指南如何在MySQL中添加用戶:逐步指南May 09, 2025 am 12:14 AM

toadduserInmysqleffect和securly,跟隨台詞:1)USEtheCreateUserStattoDaneWuser,指定thehostandastrongpassword.2)GrantNecterAryAryaryPrivilegesSustherthing privilegesgeStatement,usifementStatement,adheringtotheprinciplelastprefilegege.3)

mysql:添加具有復雜權限的新用戶mysql:添加具有復雜權限的新用戶May 09, 2025 am 12:09 AM

toaddanewuserwithcomplexpermissionsinmysql,loldtheSesteps:1)創建eTheEserWithCreateuser'newuser'newuser'@''localhost'Indedify'pa ssword';。 2)GrantreadAccesstoalltablesin'mydatabase'withGrantSelectOnMyDatabase.to'newuser'@'localhost';。 3)GrantWriteAccessto'

mysql:字符串數據類型和coltrationsmysql:字符串數據類型和coltrationsMay 09, 2025 am 12:08 AM

MySQL中的字符串數據類型包括CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT,排序規則(Collations)決定了字符串的比較和排序方式。 1.CHAR適合固定長度字符串,VARCHAR適合可變長度字符串。 2.BINARY和VARBINARY用於二進制數據,BLOB和TEXT用於大對像數據。 3.排序規則如utf8mb4_unicode_ci忽略大小寫,適合用戶名;utf8mb4_bin區分大小寫,適合需要精確比較的字段。

MySQL:我應該在Varchars上使用什麼長度?MySQL:我應該在Varchars上使用什麼長度?May 09, 2025 am 12:06 AM

最佳的MySQLVARCHAR列長度選擇應基於數據分析、考慮未來增長、評估性能影響及字符集需求。 1)分析數據以確定典型長度;2)預留未來擴展空間;3)注意大長度對性能的影響;4)考慮字符集對存儲的影響。通過這些步驟,可以優化數據庫的效率和擴展性。

mysql blob:有什麼限制嗎?mysql blob:有什麼限制嗎?May 08, 2025 am 12:22 AM

mysqlblobshavelimits:tinyblob(255bytes),blob(65,535 bytes),中間佈洛布(16,777,215個比例),andlongblob(4,294,967,967,295 bytes).tousebl觀察:1)考慮pperformance impactsandSandStorLageBlobSextern; 2)管理backbackupsandreplication carecration; 3)usepathsinst

MySQL:自動化用戶創建的最佳工具是什麼?MySQL:自動化用戶創建的最佳工具是什麼?May 08, 2025 am 12:22 AM

自動化在MySQL中創建用戶的最佳工具和技術包括:1.MySQLWorkbench,適用於小型到中型環境,易於使用但資源消耗大;2.Ansible,適用於多服務器環境,簡單但學習曲線陡峭;3.自定義Python腳本,靈活但需確保腳本安全性;4.Puppet和Chef,適用於大規模環境,複雜但可擴展。選擇時需考慮規模、學習曲線和集成需求。

mysql:我可以在斑點內搜索嗎?mysql:我可以在斑點內搜索嗎?May 08, 2025 am 12:20 AM

是的,YouCansearchInIdeAblobInMysqlusingsPecificteChniques.1)轉換theblobtoautf-8StringWithConvertFunctionWithConvertFunctionandSearchUsiseLike.2)forCompresseBlysBlobs,useuncompresseblobs,useuncompressbeforeconversion.3)expperformance impperformance imptactSandDataEcoding.4)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。