当前位置:网站首页>Chapter 8 aggregate function
Chapter 8 aggregate function
2022-07-28 05:58:00 【Big data management】
Chapter viii. Aggregate functions
Aggregate function type :AVG(),SUM(),MAX(),MIN(),COUNT()
AVG(),SUM() Only suitable for variables and fields of numeric type , String cannot be summed or averaged
SELECT AVG(salary),MAX(salary),MIN(salary),SUM(salary)
FROM employees
MIN(),MAX() For numeric types , String type , String or variable of date time type
SELECT MAX(last_name),MIN(last_name),MAX(hire_date),MIN(hire_date)
FROM employees
COUNT: The function is to calculate the number of specified fields in the query structure , It is not calculated NULL It's worth it
SELECT COUNT(employee_id),COUNT(salary),COUNT(1),COUNT(2),COUNT(*)
FROM employees
# How to calculate the number of records in a table
# Mode one :count(*) Mode two :count(1)
# Check the name and bonus rate of employees with bonuses
SELECT last_name, commission_pct
FROM employees
WHERE commission_pct IS NOT NULL
AVG=SUM/COUNT
SELECT AVG(salary),SUM(salary)/COUNT(salary),
AVG(commission_pct),SUM(commission_pct)/COUNT(commission_pct),
SUM(commission_pct)/107
FROM employees
# because count Not in the count Null value , So want to use ifnull
SELECT SUM(commission_pct)/COUNT(IFNULL(commission_pct,0)),
AVG(IFNULL(commission_pct,0))
FROM employees;
How to count records , Sure COUNT(),COUNT(*),COUNT( Use no non empty fields )
Which is more efficient
use MYISAM The efficiency of the three engines is equal
use INNODB The engine of COUNT(*)=COUNT(1) Better than COUNT( Field )
#2 group by Use : Group by field
# Check the average and maximum wages of each department
SELECT department_id,AVG(salary),SUM(salary)
FROM employees
GROUP BY department_id
ORDER BY department_id ASC;
# demand : Check each one job_id The average wage of
SELECT job_id,AVG(salary),SUM(salary)
FROM employees
GROUP BY job_id;
# demand : Ask different departments , identical job The average wage of
# Ask different departments first , Find a different job
SELECT department_id,job_id,AVG(salary)
FROM employees
GROUP BY department_id,job_id;
# perhaps : Make a difference first job_id, Ask for different departments Because the above two divisions are the same type of work and the same department number, they will be together , It's just the difference between first and then
SELECT department_id,job_id,AVG(salary)
FROM employees
GROUP BY department_id,job_id;
# The following is wrong
SELECT department_id,job_id,AVG(salary)
FROM employees
GROUP BY department_id;
# Conclusion :select The fields of non group functions appearing in must be declared in group by in
# conversely ,group by Declared fields may not appear in select in
SELECT department_id,AVG(salary)
FROM employees
GROUP BY department_id WITH ROLLUP;
# Query the average salary of each department , In ascending order of average wage
SELECT department_id,AVG(salary) avg_sal
FROM employees
GROUP BY department_id
ORDER BY avg_sal ASC;
#having effect : It is used to filter data , I have learned to filter data before where,having And group by of
# Query the highest salary ratio in each department 10000 High department information
# Demand one : Once a group function appears in the filter condition group by, Aggregate functions , Then the filter condition must be having Replace , Out of commission where;
# Requirement 2 :having It must be stated that in group by Behind ,having Must be with group by Use it together
# The following is wrong : If there are two conditions ,where And having Can be used at the same time
SELECT department_id,MAX(salary)
FROM employees
WHERE MAX(salary)>10000
GROUP BY department_id;
# The following is correct
SELECT department_id,MAX(salary)
FROM employees
GROUP BY department_id
HAVING MAX(salary)>10000;
# Inquiry Department id by 10,20,30,40 The highest wage ratio of these four departments 10000 High department information
# Mode one
SELECT department_id,MAX(salary)
FROM employees
WHERE department_id IN(10,20,30,40)
GROUP BY department_id
HAVING MAX(salary)>10000;
# Mode two
SELECT department_id,MAX(salary)
FROM employees
GROUP BY department_id
HAVING MAX(salary)>10000 AND department_id IN(10,20,30,40);
边栏推荐
猜你喜欢
随机推荐
Microsoft edge browser plug-in (2)
常见WAF拦截页面总结
(php毕业设计)基于thinkphp5校园新闻发布管理系统获取
Flex elastic box item properties
基于php学生学籍管理系统获取(php毕业设计)
cmd和npm基础命令
ArcMap map map projection related operations
第九章 子查询(重点)
JS中的!!
mysql视图,存储过程与存储函数
Acquisition of student status management system based on PHP (PHP graduation design)
路由器与交换机的区别
文旅头部结合数字藏品效应显著,但如何支撑用户持续购买力
使用sourcetree推送仓库时 Failed to connect to www.google.com port 80: Timed out
Related concepts and operations of DOM model
变量,流程控制与游标
内网信息收集总结
FlinkX安装及使用
Prime_Series靶场从探测到提权
NSCTF-web题目writeup









