SQL -> aggregation functions

Tip: Field group by and order by statements can be selected with a digital reflection
As Figure: 1 represents total_movies, 2 Representative rating
 
group by usage scenarios: the movie is a lot of people hit points, I have to look at these movies, how many people hit points
I just want to output in the form of "movie name + number of points", do not want repeated movie name, so to repeat the electricity group by using shadow group name, movie by movie name to count the score of the total score.
So with the group by generally function to use
 

SUM () function is used to count the total number of write field names in parentheses, the output value of all the fields and records
 

 
MAX () function is used to find the maximum write field names in brackets, the maximum output of all records of the field
 

MIN () function is used to find the minimum recording, use, and MAX () Likewise

 
Record the number of COUNT () function is used to calculate the number of records, field names, and write * in brackets are the output qualified
COUNT (*) null values ​​are recorded, but COUNT (field name) is not null values ​​are recorded, corresponding to different application scenarios
 

GROUP BY statement for packet coalescing the same value, and the result of the select statement we distinct deduplication similar
But GROUP BY does not filter out records, but the combined record, this time the scene generally appear in the statistics, such as maximum and minimum values ​​looking to take advantage of sql function places
The figure is from the statements made `rating` field values ​​in the table all records movies (movie)` rating` and the same number of fields (rating represents the score), that is to look at each movie is how many people got points
If there is no GROUP BY, this count (*) value will never change, always it will be the total number of records, not by rating (score) to group
 

The AVG () function is applied to calculate the matching records, the average value of a field, a field name written in parentheses
The figure statements do is to select employees working experience (experience) (employee) in more than 5 years, employees get their average salary (salary)
 

HAVING and GROUP BY statement is used together with the statement, in the absence of GROUP BY grouping, we use WHERE join finer search criteria
但是在GROUP BY按字段分组后,我们用HAVING对分组后的结果进行额外的条件约束
上图语句做的是按相同年份分组,输出年份+同年上映的电影个数,并且只选择该年上映电影数大于5的年份
HAVING对于GROUP BY,正如WHERE对于SELECT的关系,HAVING是修饰GROUP BY分组结果的语句
 

 
总结:sql函数都执行了一步计算,对于"a set of values"一堆记录进行计算,返回的是一个值

Guess you like

Origin www.cnblogs.com/exigeslover/p/12076201.html