SQL 中的聚合函數(shù)非常強大。當將這些函數(shù)與GROUP BY和 等子句組合時HAVING,我們發(fā)現(xiàn)了從全新角度查看數(shù)據(jù)的方法。我們可以使用這些函數(shù)來給我們帶來全新的見解,而不是看著同樣的舊的無休止的平板。聚合函數(shù)幫助我們理解更宏觀的事物。 這些事情可能包括查找數(shù)據(jù)集中的異常值,或者只是根據(jù)某些任意指標(例如銷售數(shù)字)確定應(yīng)該解雇哪位有家庭需要養(yǎng)活的員工。
掌握了 S的基礎(chǔ)知識JOIN后,SQL 開始變得非常非常強大。我們普通的二維表格突然獲得了這種能力,可以組合、聚合、折疊起來,像宇宙本身一樣無限向外擴展,甚至超越到第四維度。*
*需要引用
我們的基本聚合函數(shù)
首先,讓我們看看“聚合函數(shù)”是什么意思。這些簡單的函數(shù)為我們提供了一種從數(shù)學(xué)上量化數(shù)據(jù)庫中具體內(nèi)容的方法。對表列執(zhí)行聚合函數(shù),為我們提供所述列的組成。就其本身而言,它們看起來非常簡單:
AVG:列中一組值的平均值。
COUNT:指定表或視圖中一列包含的行數(shù)。
MIN:一組值中的最小值。
MAX:一組值中的最大值。
SUM:值的總和。
不同的聚合
當我們想知道值的數(shù)量時,單獨使用聚合函數(shù)的一種特別有用的方法是DISTINCT。雖然聚合值考慮了所有記錄,但使用DISTINCT限制返回的數(shù)據(jù)專門引用唯一值。COUNT(column_name)將返回列中所有記錄的數(shù)量,其中COUNT(DISTINCT column_name)將忽略計數(shù)列中重復(fù)值的記錄。
使用 GROUP BY
該GROUP BY語句通常與聚合函數(shù)(COUNT、MAX、MIN、SUM、AVG)一起使用,以按一列或多列對結(jié)果集進行分組。
為了演示聚合函數(shù)如何繼續(xù)工作,我將使用一個熟悉的數(shù)據(jù)庫:該數(shù)據(jù)庫包含本博客的所有內(nèi)容。讓我們快速預(yù)覽一下我們正在處理的內(nèi)容:
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 項:我們將使用聚合來查找哪些作者發(fā)帖最頻繁:
SELECT COUNT(title), author_idFROM postsGROUP BY author_id;
結(jié)果:
Count | author_id |
---|---|
102 | 1 |
280 | 5c12c3821345c22dced9f591 |
17 | 5c12c3821345c22dced9f592 |
5 | 5c12c3821345c22dced9f593 |
2 | 5c12c3821345c22dced9f594 |
2 | 5c12c3821345c22dced9f595 |
哦,看,一個現(xiàn)實生活中的數(shù)據(jù)問題需要解決!看起來 Ghost 的帖子表中的作者只是通過他們的 ID 來表示。這不是很有用。幸運的是,我們已經(jīng)對 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;
讓我們看看這次的結(jié)果:
Count | author_id |
---|---|
280 | Matthew Alhonte |
102 | Todd Birchard |
17 | Max Mileaf |
5 | Ryan Rosado |
2 | Graham Beckley |
2 | David Aquino |
現(xiàn)在更像了!馬特(Matt)憑借他的山貓綜述系列(Lynx Roundup )碾壓了比賽,而我則位居第二。馬克斯曾一度擁有可觀的數(shù)字,但想必已經(jīng)轉(zhuǎn)向了其他愛好,比如過自己的生活。
對于剩下的事情,除了我們正在招聘之外,我沒有什么可說的。不過我們不付錢。事實上,加入我們的好處可能為零。
使用“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;
在這種情況下,我們希望查看博客上的哪些標簽擁有最多的相關(guān)帖子。該查詢與我們之前的查詢非常相似,只是這次我們有一位特殊的客人:
HAVING COUNT(DISTINCT posts_tags.post_id) > 10
這種用法HAVING只能為我們提供具有十個或更多帖子的標簽。通過讓達爾文主義順其自然,這應(yīng)該可以清理我們的報告。結(jié)果如下:
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 |
正如預(yù)期的那樣,Matt 的綜述帖子占據(jù)領(lǐng)先地位(如果我們將其與之前的數(shù)據(jù)進行比較,我們可以看到 Matt 總共發(fā)布了17 個非 Lynx 帖子:這意味著 Max 和 Matt 正式并列)。
如果我們沒有包含我們的HAVING聲明,這個列表將會更長,充滿了沒人關(guān)心的標簽。由于明確的省略,現(xiàn)在我們不需要經(jīng)歷面對那些悲傷可悲的標簽時所帶來的黑暗抑郁。眼不見,心不煩。
更多聚合
為了探索其他一些聚合,我們將切換數(shù)據(jù)集。這次,我們將研究美國城市的風(fēng)速:
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 | 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| 4 | 3 | 0 | 3 | 4 | 4 | 0 | 3 | 3 | 6 | 3 | 3 | 2 | 1 | 8 | 2 | 2 | 2 |
讓我們想想芝加哥是否真的是風(fēng)城,好嗎?
SELECT AVG(Chicago), AVG(`San Francisco`), AVG(`Los Angeles`), AVG (Seattle), AVG(`New York`), AVG(`Boston`), AVG(Vancouver), AVG(Miami)FROM wind_speed;
...Aa結(jié)果?。?/p>
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 |
哇,這么看來(乍一看),芝加哥確實是風(fēng)最大的城市!我……不確定出于某種原因我是否在期待這一點。讓我們看看芝加哥的風(fēng)速范圍:
SELECT AVG(Chicago), MIN(Chicago), MAX(Chicago)FROM wind_speed;
AVG(Chicago) | MIN(Chicago) | MAX(Chicago) |
---|---|---|
3.7593 | 0 | 25 |
那么芝加哥數(shù)據(jù)集中的最低風(fēng)速似乎為 0(并不令人震驚)。另一方面,我們記錄的芝加哥最高風(fēng)速為25 英里/小時!哇!那不是……危險嗎?文章來源:http://www.zghlxwxcb.cn/article/577.html
發(fā)揮創(chuàng)意
聚合函數(shù)不僅僅是計算值或求平均值。特別是在數(shù)據(jù)科學(xué)中,這些函數(shù)對于從數(shù)據(jù)中得出任何統(tǒng)計結(jié)論至關(guān)重要。也就是說,注意力的持續(xù)時間有限,而且我不是科學(xué)家。也許這可以是你的工作。文章來源地址http://www.zghlxwxcb.cn/article/577.html
到此這篇關(guān)于從零開始學(xué)習(xí)SQL數(shù)據(jù)庫操作:學(xué)習(xí)聚合函數(shù)的使用方法的文章就介紹到這了,更多相關(guān)內(nèi)容可以在右上角搜索或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!