[Turn] use of SQL statements in the GROUP BY and HAVING statement

一、GROUP BY

GROUP BY statement to the aggregate function (aggregate functions such as COUNT, SUM, AVG, MIN, or MAX.) Or in combination to obtain a result set of a plurality of columns.

The syntax is as follows:

SELECT column1, column2, ... column_n, aggregate_function (expression)            

FROM tables            

WHERE predicates            

GROUP BY column1, column2, ... column_n;

 

For example

For example, we have a student form (student), contains student number (id), curriculum (course), score (score) and so on more than one column, we want each student chose a few courses by querying get, then we COUNT function can be used in conjunction with GROUP bY statement to get this result

SELECT id, COUNT(course) as numcourse

FROM student

GROUP BY id

, You can calculate the corresponding number for each school by the COUNT (course) Lessons learned because we are using numbers to group, so the COUNT function is the premise of school number of packets down to achieve.

 

note

Because the aggregate functions to return a set of data only by the action of a single value, therefore, the element appearing in a SELECT statement is either the input value aggregation function, or parameters for the GROUP BY statement, or an error occurs.

For example, for a table mentioned above, we do a query like:

SELECT id, COUNT(course) as numcourse, score

FROM student

GROUP BY id

At this inquiry will be mistakes, the following error message:

Column ‘student.score' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

The reason for the above error because a student id corresponding number of points, if we simply write the score in the SELECT statement, you can not determine which one should score output. To score a select statement as it may be used as an input parameter value is a function of the polymerization, the following example, we can get the number of courses, and the average score for each student each student selected:

SELECT id, COUNT(course) as numcourse, AVG(score) as avgscore

FROM student

GROUP BY id

 

二、HAVING

HAVING statement GROUP BY generally used in combination statement, to filter by the GROUP BY recordset returned statement.

HAVING presence WHERE statement to make up for lack of keyword can not be used in conjunction with aggregate functions.

grammar:

SELECT column1, column2, ... column_n, aggregate_function (expression)
FROM tables
WHERE predicates
GROUP BY column1, column2, ... column_n
HAVING condition1 ... condition_n;

Students also use the table in this article, if you want to query the average score higher than 80 points of student records can be written:

SELECT id, COUNT(course) as numcourse, AVG(score) as avgscore

FROM student

GROUP BY id

HAVING AVG(score)>=80;

Here, if the WHERE instead of HAVING error occurs

Reproduced: https://www.cnblogs.com/8335IT/p/5850531.html .

Guess you like

Origin www.cnblogs.com/day1day1up/p/11277624.html