`
flysky
  • 浏览: 65361 次
社区版块
存档分类
最新评论

Group by ,Having, Aggregate functions and top 10

    博客分类:
  • java
阅读更多
1. 今天实验的收获很大,最重要的就是Mr dennis帮我指出了我的一个严重错误!the serious error about querying the ten items having the least total quantities.
I didn’t understand the use of “group by” and “having” until now! They are just and mainly used for aggregate functions.
2.  http://www.w3schools.com/sql/sql_groupby.asp
SQL GROUP BY and HAVING
________________________________________
Aggregate functions (like SUM) often need an added GROUP BY functionality.
________________________________________
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.
The syntax for the GROUP BY function is:
SELECT column,SUM(column) FROM table GROUP BY column

________________________________________
GROUP BY Example
This "Sales" Table:
Company Amount
W3Schools 5500
IBM 4500
W3Schools 7100
And This 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:
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.
The syntax for the HAVING function is:
SELECT column,SUM(column) FROM table
GROUP BY column
HAVING SUM(column) condition value
This "Sales" Table:
Company Amount
W3Schools 5500
IBM 4500
W3Schools 7100
This SQL:
SELECT Company,SUM(Amount) FROM Sales
GROUP BY Company
HAVING SUM(Amount)>10000
Returns this result
Company SUM(Amount)
W3Schools 12600

2. http://mycodeblog.blogspot.com/2006/11/how-to-select-top-results-in-group-by.html
How to Select Top Results in a Group By to use Microsoft SQL Server:
SET ROWCOUNT 10
SELECT field1, field2, COUNT(*)
FROM table
GROUP BY field1, field2
ORDER BY COUNT(*) DESC

3. http://www.kekecn.com/blog/article.asp?id=146
Microsoft SQL Server
select top 3 e_name,e_wage from employee order by e_wage desc
分享到:
评论

相关推荐

    《Oracle Database编程指南》之15:聚合函数(Aggregate Functions)

    聚合函数可以出现在SELECT列表中,也可以出现在ORDER BY和HAVING子句中,它们通常与SELECT语句中的GROUP BY子句一起使用,但这不是强制的。 首先,在SELECT语句中,Oracle数据库将查询的表或视图的行划分为组。 如果...

    SQL语句-递归查询、分组、统计.pdf

    - 复杂的`GROUP BY`:`SELECT t.personname, t.idno, COUNT(idno) FROM hr_returnsale t GROUP BY idno HAVING COUNT(idno) > 1`,它同时考虑了`personname`,并仅显示那些与多个`idno`关联的人员。 3. **统计...

    SAS.9.2.SQL.Procedure.Users.Guide

    After grouping data, you can further filter the results using the HAVING clause, which filters the results of a GROUP BY operation. For example: ```sql SELECT column1, COUNT(*) FROM table_name GROUP ...

    salesforce soql sosl技术文档

    例如,`SELECT COUNT(Id), Department FROM Contact GROUP BY Department HAVING COUNT(Id) > 10`。 - **Considerations When Using HAVING**:与`WHERE`子句不同,`HAVING`子句只能应用于已分组的数据。 ##### ...

    SQL的简单查询功能

    4. **聚集函数(Aggregate Functions)** - 聚集函数用于计算一组值的结果,如计数(`COUNT`)、平均值(`AVG`)、最大值(`MAX`)、最小值(`MIN`)等。 - 示例:查询每个系有多少个同学。 ```sql SELECT sdept, COUNT...

    SQL 经典查询练习

    以上示例涵盖了SQL查询的基础语法和一些高级技巧,如连接操作(JOIN)、子查询(SUBQUERY)和聚合函数(AGGREGATE FUNCTIONS)。通过这些练习,可以帮助初学者更好地理解和掌握SQL语言的关键概念和技术,从而提高其...

    微软内部资料-SQL性能优化5

     Chapter 11: “Batches, Stored Procedures and Functions”, Inside SQL Server 2000 by Kalen Delaney Finding Rows without Indexes With No Indexes, A Table Must Be Scanned SQL Server keeps track ...

    oracle 查询部分ppt

    在Oracle中,SQL查询的基本结构包括SELECT、FROM、WHERE、GROUP BY、HAVING、ORDER BY等子句。其中,SELECT用于定义要查询的列,FROM指定要查询的表,WHERE用于设定查询条件,GROUP BY用于数据分组,HAVING用于分组...

    oracle最经典练习题及答案

    4. 分组与聚合(Group By and Aggregation):使用GROUP BY对员工按部门分组,并使用聚合函数(如avg)计算每个部门的平均薪资,例如:“group by e.dept_id having avg(salary)>(select avg(salary)...)”。...

    大型数据库期末试题以及答案.docx

    3. 分组查询(Group By):要找到拥有超过10个顾客的国家,应使用`GROUP BY`与`HAVING`子句。正确答案是A,`SELECT Country FROM Customers GROUP BY Country HAVING COUNT (Country) > 10`。选项B只选择了前10个...

    SQL_chapter3.rar

    10. **存储过程和函数(Stored Procedures and Functions)**:学习如何定义和调用存储过程和自定义函数,以执行复杂的数据库操作。 以上内容仅是对SQL第三章可能涵盖的主题的概述,具体内容可能会根据不同的教程或...

    oracle database

    ### Oracle Database: Aggregate Functions and Sub-queries 在Oracle数据库的学习过程中,掌握基本的表操作、SQL查询技巧是非常重要的。特别是对于新手来说,通过实践来学习是最佳的方式之一。本篇文章将根据提供...

    开发中的sql积累

    `HAVING`子句通常与`GROUP BY`一起使用,对分组后的数据进行过滤。 总的来说,SQL的DISTINCT关键字和各种函数极大地增强了我们处理数据库的能力,使我们能够进行复杂的分析和数据清洗。在实际开发中,熟练掌握这些...

    简单了解mysql语句书写和执行顺序

    - `AGGREGATE FUNCTIONS`: 如`SUM()`, `COUNT()`, `AVG()`等,用于分组计算。 - `HAVING`: 分组后的筛选。 - `SELECT`: 选择要返回的列,如果列未出现在`GROUP BY`中,必须是聚合函数。 - `DISTINCT`: 去除重复...

    【转载】oracle笔试4.doc

    `SELECT job_id, SUM(salary) FROM emp_dept_vu WHERE department_id IN (10,20) GROUP BY job_id HAVING SUM(salary) > 20000;` 这个查询将筛选出部门ID为10或20的职位,然后按职位ID分组,只显示那些总薪水大于...

    SQL语法大全中文版.rar_sql_sql 语法_sql server_sql 语法_sql语法

    `SELECT`语句是最基础的查询语句,可以配合`FROM`、`WHERE`、`GROUP BY`、`HAVING`、`ORDER BY`等子句进行复杂的数据筛选、分组和排序。 4. **数据控制语言(DCL)**:涉及到权限和访问控制。`GRANT`用于赋予用户或...

    图书管理系统1

    11. **聚合函数 (Aggregate Functions)**: 查询各出版社图书的最高价、最低价、平均价和总价: ```sql SELECT 出版单位, MAX(单价) AS 最高价, MIN(单价) AS 最低价, AVG(单价) AS 平均价, SUM(单价) AS 总价 ...

    数据库使用者的良药-SQL精华操作

    通过指定列名、使用WHERE子句过滤条件、GROUP BY进行分组以及HAVING和ORDER BY进行结果筛选和排序,可以实现复杂的数据查询需求。 其次,INSERT INTO语句用于向数据库中添加新记录。这涉及到对表格结构的理解,包括...

    SQL参考手册

    - 分组与聚合(GROUP BY & HAVING):GROUP BY用于根据一个或多个列对结果集进行分组,HAVING则用于筛选分组后的结果。 - JOIN操作:INNER JOIN、LEFT JOIN、RIGHT JOIN和FULL OUTER JOIN用于合并来自两个或更多表...

Global site tag (gtag.js) - Google Analytics