Aggregate functions (like SUM) often need an added GROUP BY functionality.

集合函数(类似SUM)经常需要用GROUP BY来进行功能性的补充。

GROUP BY...

GROUP BY... was added to SQL because aggregate functions (like SUM) return the aggregate of all column values every time they are called, and without the GROUP BY function it was impossible to find the sum for each individual group of column values.

GROUP BY...之所以加到SQL中去是因为集合函数(像SUM)每当他们被访问时就会返回集合所有栏目的值,而且没有GROUP BY的话就不能够找出单独一种栏目所累计的值了。

The syntax for the GROUP BY function is:

使用GROUP BY函数的语法为:

SELECT column,SUM(column) FROM table GROUP BY column

GROUP BY Example

举例

This "Sales" Table:

这是张名为"Sales"的表: Company Amount 
W3Schools 5500 
IBM 4500 
W3Schools 7100

And This SQL:

这是条SQL:

SELECT Company, SUM(Amount) FROM Sales

Returns this result:

返回的结果为: Company SUM(Amount) 
W3Schools 17100 
IBM 17100 
W3Schools 17100

The above code is invalid because the column returned is not part of an aggregate. A GROUP BY clause will solve this problem:

上面这些代码几乎是无效的,因为栏目所返回的数值并不属于我们想要的那种合计。使用 GROUP BY子句可以解决这个问题:

SELECT Company,SUM(Amount) FROM Sales

GROUP BY Company

Returns this result:

返回的结果为: Company SUM(Amount) 
W3Schools 12600 
IBM 4500

HAVING...

HAVING... was added to SQL because the WHERE keyword could not be used against aggregate functions (like SUM), and without HAVING... it would be impossible to test for result conditions.

WHERE关键字在使用集合函数时不能使用,所以在集合函数中加上了HAVING来起到测试查询结果是否符合条件的作用。

The syntax for the HAVING function is:

HAVING的使用语法为:

SELECT column,SUM(column) FROM table

GROUP BY column

HAVING SUM(column) condition value

This "Sales" Table:

这是名为"Sales"的表: Company Amount 
W3Schools 5500 
IBM 4500 
W3Schools 7100

This SQL:

SQL语句:

SELECT Company,SUM(Amount) FROM Sales

GROUP BY Company

HAVING SUM(Amount)>10000

Returns this result

返回的结果为 Company SUM(Amount) 
W3Schools 12600

原文链接:http://shawnccx.iteye.com/blog/739364

最新文章

  1. 使用adb shell 进入手机修改文件的权限
  2. CSS/HTML 改变鼠标指针形状
  3. 学习WebSocket(二):使用Spring WebSocket做一个简单聊天室
  4. Configuring a Windows Azure Project
  5. HTTP 简介
  6. LINUX下中文语言包的安装(转)
  7. macos10.8.5原版系统dmg转iso
  8. 动态生成WebService的客户端
  9. Centos 7 拨号上网(PPPOE)
  10. python机器学习实战(四)
  11. 可靠联机的 TCP 协议
  12. Google Python命名规范
  13. 深入理解JVM - 1 - Java内存区域划分
  14. light1341 唯一分解定理
  15. Java的程序执行过程与编译原理
  16. redis 配置初体验
  17. Servlet简介及其生命周期详解
  18. [转][osg][QT]osg与QT界面结合的简单例子
  19. Qt5数据库
  20. webpack 搭建问题汇总

热门文章

  1. Celery-4.1 用户指南: Application(应用)
  2. 清除SUN服务器部件的suspect状态
  3. C#高级参数params的使用
  4. strophe与openfire模拟的XMPP简单hello程序
  5. 什么是个CDN???CDN是干什么的??
  6. sharepoint 2013创建网站集,域帐户无法访问,只有administrator可以访问
  7. 用position: sticky 实现粘性元素区域悬浮效果(转)
  8. linux中创建图片服务器减轻传统服务器的压力
  9. Codeforces 1097F Alex and a TV Show (莫比乌斯反演)
  10. WCF客户端调用并行最大同时只支持两个请求