Skip to Content
《SQL 烹饪书》第二版
book

《SQL 烹饪书》第二版

by Anthony Molinaro, Robert de Graaf
May 2025
Intermediate to advanced
570 pages
7h 38m
Chinese
O'Reilly Media, Inc.
Content preview from 《SQL 烹饪书》第二版

第 7 章 使用数字 使用数字

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

本章重点介绍涉及数字的常见操作,包括数值计算。虽然 SQL 通常不被认为是复杂计算的首选,但对于日常的数字杂务来说,它却是高效的。更重要的是,由于支持 SQL 的数据库和数据仓库可能仍然是查找组织数据的最常见的地方,因此使用 SQL 来探索和评估这些数据对于将这些数据用于工作的任何人来说都是必不可少的。本节中选择的技术还可以帮助数据科学家决定哪些数据最有希望进行进一步分析。

提示

本章中的一些配方使用了聚合函数和 GROUP BY 子句。如果不熟悉分组,请至少阅读附录 A 中名为 "分组 "的第一大节。

7.1 计算平均数

问题

您要计算一列的平均值,可以是表中所有行的平均值,也可以是某些行子集的平均值。例如,您可能想找出所有员工的平均工资以及每个部门的平均工资。

解决方案

计算所有员工工资的平均值时,只需对包含这些工资的列应用 AVG 函数即可。

排除 WHERE 子句后,平均值将根据所有非空值计算:

1 select avg(sal) as avg_sal
2   from emp

   AVG_SAL
----------
2073.21429

要计算每个部门的平均工资,请使用 GROUP BY 子句创建一个与每个部门相对应的组:

1 select deptno, avg(sal) as avg_sal
2   from emp
3  group by deptno

    DEPTNO     AVG_SAL
----------  ----------
        10  2916.66667
        20        2175
        30  1566.66667

讨论

在以整个表为组或窗口查找平均值时,只需对感兴趣的列应用 AVG 函数,而无需使用 GROUP BY 子句。需要注意的是,函数 AVG 会忽略 NULL。这里可以看到忽略 NULL 值的效果:

create table t2(sal integer)
insert into t2 values (10)
insert into t2 values (20)
insert into t2 values (null)
select avg(sal)    select distinct 30/2
  from t2            from t2

  AVG(SAL)               30/2
----------         ----------
        15                 15


select avg(coalesce(sal,0))    select distinct 30/3
  from t2                        from t2

AVG(COALESCE(SAL,0))                 30/3
--------------------           ----------
                  10                   10

COALESCE 函数将返回所传递数值列表中的第一个非空值。当 NULL SAL 值转换为零时,平均值会发生变化。调用聚合函数时,请务必考虑如何处理 NULL。

解决方案的第二部分使用 GROUP BY(第 3 行)根据部门归属将员工记录分成不同的组。GROUP BY 会自动执行诸如 AVG 等聚合函数,并返回每个组的结果。在本例中,AVG 会为每个基于部门的员工记录组执行一次。

顺便说一下,在选择列表中不必包含 GROUP BY 列。例如

select avg(sal)
  from emp
 group by deptno

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

尽管 DEPTNO ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

SQL Essentials For Dummies

SQL Essentials For Dummies

Richard Blum, Allen G. Taylor
Oracle SQL By Example

Oracle SQL By Example

Alice Rischert

Publisher Resources

ISBN: 9798341658813