从零开始学习SQL数据库操作:学习聚合函数的使用方法

1,473次阅读
没有评论

共计 5767 个字符,预计需要花费 15 分钟才能阅读完成。

SQL 中的聚合函数非常强大。当将这些函数与 GROUP BY 和 等子句组合时 HAVING,我们发现了从全新角度查看数据的方法。我们可以使用这些函数来给我们带来全新的见解,而不是看着同样的旧的无休止的平板。聚合函数帮助我们理解更宏观的事物。这些事情可能包括查找数据集中的异常值,或者只是根据某些任意指标(例如销售数字)确定应该解雇哪位有家庭需要养活的员工。

掌握了 S 的基础知识 JOIN 后,SQL 开始变得非常非常强大。我们普通的二维表格突然获得了这种能力,可以组合、聚合、折叠起来,像宇宙本身一样无限向外扩展,甚至超越到第四维度。*

* 需要引用

我们的基本聚合函数

首先,让我们看看“聚合函数”是什么意思。这些简单的函数为我们提供了一种从数学上量化数据库中具体内容的方法。对表列执行聚合函数,为我们提供所述列的组成。就其本身而言,它们看起来非常简单:

  • AVG:列中一组值的平均值。

  • COUNT:指定表或视图中一列包含的行数。

  • MIN:一组值中的最小值。

  • MAX:一组值中的最大值。

  • SUM:值的总和。

不同的聚合

当我们想知道值的数量时,单独使用聚合函数的一种特别有用的方法是 DISTINCT。虽然聚合值考虑了所有记录,但使用 DISTINCT 限制返回的数据专门引用唯一值。COUNT(column_name) 将返回列中所有记录的数量,其中 COUNT(DISTINCT column_name) 将忽略计数列中重复值的记录。

使用 GROUP BY

该 GROUP BY 语句通常与聚合函数(COUNT、MAX、MIN、SUM、AVG)一起使用,以按一列或多列对结果集进行分组。

为了演示聚合函数如何继续工作,我将使用一个熟悉的数据库:该数据库包含本博客的所有内容。让我们快速预览一下我们正在处理的内容:

title slug feature_image meta_title meta_description created_at updated_at published_at custom_excerpt
Welcome to Hackers and Slackers welcome-to-hackers-and-slackers /content/images/2017/11/welcome@2x.jpg Welcome to Hackers and Slackers | Hackers and Slackers Technology for badasses 2017-11-17 20:29:13 2018-07-25 02:06:02 2017-11-13 20:37:00 Technology for badasses.
Generating Tree Hierarchies with Treelib creating-trees-in-treelib /content/images/2017/11/tree7@2x.jpg Tree Hierarchies with Treelib | Hackers and Slackers Treelib is a Python library that allows you to create a visual tree hierarchy: a simple plaintext representation of parent-child relationships. 2017-11-17 20:45:10 2019-03-28 09:02:39 2017-11-17 20:56:40 Using Python to visualize file hierarchies as trees.
About the Squad about https://hackers.nyc3.cdn.digitaloceanspaces.com/posts/2017/11/welcome@2x.jpg About | Hackers and Slackers Hackers and Slackers is a community which values technology, life, and improving the latter with the former. 2017-11-17 20:58:42 2019-04-22 08:47:02 2017-11-17 20:58:46 Hackers and Slackers is a community which values technology, life, and improving the latter with the former.
Join join https://hackers.nyc3.cdn.digitaloceanspaces.com/posts/2017/11/join@2x.jpg Join | Hackers and Slackers 2017-11-17 20:59:05 2018-07-25 02:06:02 2017-11-17 21:03:06
Merge Sets of Data in Python Using Pandas merge-dataframes-with-pandas /content/images/2017/11/pandasmerge@2x.jpg Merging Dataframes with Pandas | Hackers and Slackers Perform merges of data similar to SQL JOINs using Python’s Pandas library: the essential library for data analysis in Oython. 2017-11-18 00:09:32 2018-12-26 09:29:22 2017-11-18 00:22:25 Perform SQL-like merges of data using Python’s Pandas.

我们议程上的第 1 项:我们将使用聚合来查找哪些作者发帖最频繁:

SELECT
  COUNT(title), author_idFROM
  postsGROUP BY author_id;

结果:

Count author_id
102 1
280 5c12c3821345c22dced9f591
17 5c12c3821345c22dced9f592
5 5c12c3821345c22dced9f593
2 5c12c3821345c22dced9f594
2 5c12c3821345c22dced9f595

哦,看,一个现实生活中的数据问题需要解决!看起来 Ghost 的帖子表中的作者只是通过他们的 ID 来表示。这不是很有用。幸运的是,我们已经对 JOIN 有了足够的了解,知道我们可以填充 users 表中缺失的信息!

SELECT
  COUNT(posts.title),
  users.nameFROM
  postsLEFT JOIN usersON
  (posts.author_id = users.id)GROUP BY users.idORDER BY COUNT(posts.title) DESC;

让我们看看这次的结果:

Count author_id
280 Matthew Alhonte
102 Todd Birchard
17 Max Mileaf
5 Ryan Rosado
2 Graham Beckley
2 David Aquino

现在更像了!马特(Matt)凭借他的山猫综述系列(Lynx Roundup)碾压了比赛,而我则位居第二。马克斯曾一度拥有可观的数字,但想必已经转向了其他爱好,比如过自己的生活。

对于剩下的事情,除了我们正在招聘之外,我没有什么可说的。不过我们不付钱。事实上,加入我们的好处可能为零。

使用“HAVING”进行条件分组

HAVING 就像 WHERE 聚合的一样。我们不能使用 WHERE 聚合值,所以这就是 HAVING 存在的原因。HAVING 不能接受任何条件值,但它必须接受从 GROUP BY. 也许这在查询中更容易可视化:

SELECT
  tags.name,
  COUNT(DISTINCT posts_tags.post_id)FROM posts_tags  LEFT JOIN tags ON tags.id = posts_tags.tag_id  LEFT JOIN posts ON posts.id = posts_tags.post_idGROUP BY
  tags.idHAVING
  COUNT(DISTINCT posts_tags.post_id) > 10ORDER BY
  COUNT(DISTINCT posts_tags.post_id)
  DESC;

在这种情况下,我们希望查看博客上的哪些标签拥有最多的相关帖子。该查询与我们之前的查询非常相似,只是这次我们有一位特殊的客人:

HAVING
  COUNT(DISTINCT posts_tags.post_id) > 10

这种用法 HAVING 只能为我们提供具有十个或更多帖子的标签。通过让达尔文主义顺其自然,这应该可以清理我们的报告。结果如下:

tag Count
Roundup 263
Python 80
Machine Learning 29
DevOps 28
Data Science 28
Software Development 27
Data Engineering 23
Excel 19
SQL 18
Architecture 18
REST APIs 16
#Adventures in Excel 16
Pandas 15
Flask 14
Data Analysis 12
JavaScript 12
AWS 11
MySQL 11

正如预期的那样,Matt 的综述帖子占据领先地位(如果我们将其与之前的数据进行比较,我们可以看到 Matt 总共发布了 17 个非 Lynx 帖子:这意味着 Max 和 Matt 正式并列)。

如果我们没有包含我们的 HAVING 声明,这个列表将会更长,充满了没人关心的标签。由于明确的省略,现在我们不需要经历面对那些悲伤可悲的标签时所带来的黑暗抑郁。眼不见,心不烦。

更多聚合

为了探索其他一些聚合,我们将切换数据集。这次,我们将研究美国城市的风速:

datetime Vancouver Portland San Francisco Seattle Los Angeles San Diego Las Vegas Phoenix Albuquerque Denver San Antonio Dallas Houston Kansas City Minneapolis Saint Louis Chicago Nashville Indianapolis Atlanta Detroit Jacksonville Charlotte Miami Pittsburgh Toronto Philadelphia New York Montreal Boston Beersheba Tel Aviv District Eilat Haifa Nahariyya Jerusalem
2012-10-01 12:00:00 8
2012-10-01 13:00:00 0 0 2 0 0 0 0 2 4 4 0 3 1 0 3 4 0 4 4 3 0 3 4 3 0 3 4 7 4 3 1 0 8 2 2 2
2012-10-01 14:00:00 0 0 2 0 0 0 0 2 4 4 0 3 1 0 3 4 0 4 4 3 0 3 4 3 0 3 4 7 4 3 3 0 8 2 2 2
2012-10-01 15:00:00 0 0 2 0 0 0 0 2 4 3 0 3 1 0 3 4 0 4 4 3 0 3 4 3 0 3 3 7 4 3 3 0 8 2 2 2
2012-10-01 16:00:00 0 0 2 0 0 0 0 2 4 3 0 3 1 0 3 3 0 4 4 3 0 3 4 3 0 3 3 7 4 3 3 0 8 2 2 2
2012-10-01 17:00:00 0 0 2 0 0 0 0 2 4 3 0 3 1 0 3 3 0 4 4 3 0 3 4 3 0 3 3 6 3 3 3 0 8 2 2 2
2012-10-01 18:00:00 0 0 2 0 0 0 0 2 4 3 0 3 2 0 3 3 0 4 4 3 0 3 4 3 0 3 3 6 3 3 3 0 8 2 2 2
2012-10-01 19:00:00 0 0 2 0 0 0 0 2 4 3 0 3 2 0 3 3 0 4 4 3 0 3 4 4 0 3 3 6 3 3 2 1 8 2 2 2
2012-10-01 20:00:00 0 0 1 0 0 0 0 1 4 3 0 3 2 0 3 3 0 4 4 3 0 3 4 4 0 3 3 6 3 3 2 1 8 2 2 2
2012-10-01 21:00:00 0 0 1 0 0 0 0 1 4 3 0 3 2 0 3 3 0 4 4 3 0 3 4 4 0 3 3 6 3 3 2 1 8 2 2 2

让我们想想芝加哥是否真的是风城,好吗?

SELECT
	AVG(Chicago),
	AVG(`San Francisco`),
	AVG(`Los Angeles`),
	AVG (Seattle),
	AVG(`New York`),
	AVG(`Boston`),
	AVG(Vancouver),
	AVG(Miami)FROM
	wind_speed;

…Aa 结果!:

AVG(Chicago) AVG(`San Francisco`) AVG(`Los Angeles`) AVG (Seattle) AVG(`New York`) AVG(`Boston`) AVG(Vancouver) AVG(Miami)
3.7593 2.7867 1.2195 2.1181 3.2110 3.3809 2.4327 3.2365

哇,这么看来(乍一看),芝加哥确实是风最大的城市!我……不确定出于某种原因我是否在期待这一点。让我们看看芝加哥的风速范围:

SELECT
	AVG(Chicago),
	MIN(Chicago),
	MAX(Chicago)FROM
	wind_speed;
AVG(Chicago) MIN(Chicago) MAX(Chicago)
3.7593 0 25

那么芝加哥数据集中的最低风速似乎为 0(并不令人震惊)。另一方面,我们记录的芝加哥最高风速为 25 英里 / 小时!哇!那不是……危险吗?

发挥创意

聚合函数不仅仅是计算值或求平均值。特别是在数据科学中,这些函数对于从数据中得出任何统计结论至关重要。也就是说,注意力的持续时间有限,而且我不是科学家。也许这可以是你的工作。 文章来源地址 https://www.toymoban.com/diary/sql/577.html

到此这篇关于从零开始学习 SQL 数据库操作:学习聚合函数的使用方法的文章就介绍到这了, 更多相关内容可以在右上角搜索或继续浏览下面的相关文章,希望大家以后多多支持 TOY 模板网!

原文地址:https://www.toymoban.com/diary/sql/577.html

如若转载,请注明出处:如若内容造成侵权 / 违法违规 / 事实不符,请联系站长进行投诉反馈,一经查实,立即删除!

    正文完
     0
    Yojack
    版权声明:本篇文章由 Yojack 于1970-01-01发表,共计5767字。
    转载说明:
    1 本网站名称:优杰开发笔记
    2 本站永久网址:https://yojack.cn
    3 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
    4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
    5 本站所有内容均可转载及分享, 但请注明出处
    6 我们始终尊重原创作者的版权,所有文章在发布时,均尽可能注明出处与作者。
    7 站长邮箱:laylwenl@gmail.com
    评论(没有评论)