sql sum语句_SQL Sum语句示例说明

sql sum语句

SQL中的Sum语句是什么? (What is the Sum statement in SQL?)

This is one of the aggregate functions (as is count, average, max, min, etc.). They are used in a GROUP BY clause as it aggregates data presented by the SELECT FROM WHERE portion of the statement.

这是合计函数之一(如计数,平均值,最大值,最小值等)。 它们在GROUP BY子句中使用,因为它聚集了语句的SELECT FROM WHERE部分提供的数据。

使用例 (Example of use)

“sum(Total_$)” in the SELECT statement is aggregated in the GROUP BY clause. “Count(*)” provides the number of contributions.

SELECT语句中的“ sum(Total_ $)”汇总在GROUP BY子句中。 “ Count(*)”提供了捐款数量。

This data is from the campaign contributions data we’ve been using in some of these guides.

此数据来自我们在其中一些指南中一直使用的广告系列贡献数据。

This SQL statement is answering the question: “which candidates received the largest total contribution dollars in 2016 BUT only those that had more than $20 Million USD for all contributions combined?”

该SQL语句回答了以下问题:“哪些候选人在2016年BUT中获得的捐款总额最多,而所有捐款加起来超过2000万美元的候选人呢?”

Ordering this data set in a descending (DESC) order places the candidates with the largest total contributions at the top of the list.

按降序(DESC)排序此数据集,将总贡献最大的候选者放在列表的顶部。

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.
+--------------------------------------------------+---------------+-------------------+----------+
| 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)

As with all of these SQL things there is MUCH MORE to them than what’s in this introductory guide.

与所有这些SQL事物一样,它们比本入门指南中的内容要多得多。

I hope this at least gives you enough to get started.

我希望这至少能给您足够的入门。

Please see the manual for your database manager and have fun trying different options yourself.

请参阅数据库管理员的手册,并尝试自己尝试其他选项,这很有趣。

翻译自: https://www.freecodecamp.org/news/sql-sum-statements-explained-with-examples/

sql sum语句

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值