Home Database Mysql Tutorial Oracle基础教程:聚集、分组、行转列

Oracle基础教程:聚集、分组、行转列

Jun 07, 2016 pm 05:11 PM

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Apr 02, 2025 pm 07:05 PM

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.

RDS MySQL integration with Redshift zero ETL RDS MySQL integration with Redshift zero ETL Apr 08, 2025 pm 07:06 PM

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

See all articles