freeCodeCamp/guide/chinese/sql/sql-sum-function/index.md

47 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: SQL Sum Function
localeTitle: SQL求和函数
---
## SQL求和函数
这是聚合函数之一如计数平均值最大值最小值等。它们在GROUP BY子句中使用因为它聚合语句的SELECT FROM WHERE部分所呈现的数据。
### 使用示例
SELECT语句中的“sumTotal\_ $”聚合在GROUP BY子句中。 “Count\*)”提供了贡献的数量。
这些数据来自我们在其中一些指南中使用的广告系列贡献数据。
这个SQL语句正在回答这个问题“哪些候选人在2016年获得了最大的总捐款额但只有那些超过2000万美元的所有捐款合计
以降序DESC顺序对此数据集进行排序会将总贡献最大的候选项放在列表顶部。
```sql
SELECT Candidate, Election_year, sum(Total_$), count(*)
FROM combined_party_data
WHERE Election_year = 2016
GROUP BY Candidate, Election_year -- this tells the DBMS to summarize by these two columns
HAVING sum(Total_$) > 20000000 -- limits the rows presented from the summary of money ($20 Million USD)
ORDER BY sum(Total_$) DESC; -- orders the presented rows with the largest ones first.
```
```text
+--------------------------------------------------+---------------+-------------------+----------+
| Candidate | Election_year | sum(Total_$) | count(*) |
+--------------------------------------------------+---------------+-------------------+----------+
| CLINTON, HILLARY RODHAM & KAINE, TIMOTHY M (TIM) | 2016 | 568135094.4400003 | 126 |
| TRUMP, DONALD J & PENCE, MICHAEL R (MIKE) | 2016 | 366853142.7899999 | 114 |
| SANDERS, BERNARD (BERNIE) | 2016 | 258562022.17 | 122 |
| CRUZ, RAFAEL EDWARD (TED) | 2016 | 93430700.29000005 | 104 |
| CARSON, BENJAMIN S (BEN) | 2016 | 62202411.12999996 | 93 |
| RUBIO, MARCO ANTONIO | 2016 | 44384313.9 | 106 |
| BUSH, JOHN ELLIS (JEB) | 2016 | 34606731.78 | 97 |
+--------------------------------------------------+---------------+-------------------+----------+
7 rows in set (0.01 sec)
```
与所有这些SQL事物一样它们比本入门指南中的内容更多。
我希望这至少足以让你开始。
请参阅您的数据库管理员手册,并自己尝试不同的选项。