当前位置:网站首页>Use of aggregate functions
Use of aggregate functions
2022-07-03 19:35:00 【Piglet get】
Catalog
One 、 Aggregate function introduction
Ⅰ. How to realize the number of records in the calculation table ?
Ⅲ. Query the average bonus rate in the company
Use count(*),count(1),count( Specific fields ) Which is more efficient ?
demand 1: Query the average salary of each department , Maximum wage
demand 2: Check each one department_id,job_id The average wage of
2. practice : Query the highest salary ratio in each department 10000 High department information
Four 、SQL The underlying execution principle
( One )WHERE And HAVING Comparison of
2. If the filter condition has no aggregate function
( 3、 ... and )SQL Statement execution
One 、 Aggregate function introduction
1. Concept
Aggregate functions act on a set of data , And return a value for a set of data .
2. Type of aggregate function
①AVG and SUM function
Only applicable to fields of numeric type ( Or variable )
SELECT AVG(salary),SUM(salary),AVG(salary)*107
FROM employees;
# The following operations are meaningless
SELECT SUM(last_name),AVG(last_name),SUM(hire_date)
FROM employees;
②MIN and MAX function
For numeric types 、 String type 、 Fields of date time type ( Or variable )
SELECT MAX(salary),MIN(salary),MAX(hire_date),MIN(hire_date)
FROM employees;
③COUNT function
Calculate the number of specified fields in the query structure
SELECT COUNT(employee_id),COUNT(salary),COUNT(2*salary),COUNT(1)
FROM employees;
Ⅰ. How to realize the number of records in the calculation table ?
The way 1:COUNT(*)
The way 2:COUNT(1)
The way 3:COUNT( Specific fields ) —— Not necessarily right
Be careful : When calculating the number of occurrences of the specified field , It is not calculated NULL It's worth it
Ⅱ.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;
Ⅲ. Query the average bonus rate in the company
# Wrong writing
SELECT AVG(commission_pct)
FROM employees;
# Write it correctly
SELECT SUM(commission_pct) / COUNT(IFNULL(commission_pct,0)),
AVG(IFNULL(commission_pct,0))
FROM employees;
Use count(*),count(1),count( Specific fields ) Which is more efficient ?
If you are using MYISAM Storage engine , Then the efficiency of the three is the same , All are O(1)
If you are using InnoDB Storage engine , Then the efficiency of the three count(*) = count(1) > count( Specific fields )
Two 、GROUP BY Use
demand 1: Query the average salary of each department , Maximum wage
SELECT AVG(salary)
FROM employees; # The average salary of the whole company
SELECT department_id,AVG(salary),MAX(salary)
FROM employees
GROUP BY department_id;
demand 2: Check each one department_id,job_id The average wage of
SELECT department_id,job_id,AVG(salary)
FROM employees
GROUP BY department_id,job_id;
Conclusion
1.SELECT Fields of non group functions appearing in , It must be stated that in GROUP BY in . conversely ,GROUP BY Fields declared in can not appear in SELECT in
2.GROUP BY The statement in FROM Back 、WHERE Back ,ORDER BY front 、LIMIT front
3. stay MySQL Use in WITH ROLLUP: Add a record after all the queried grouping records , This record Calculate the sum of all the records queried , That is, count the number of records .
3、 ... and 、HAVING Use
1. effect
Filtering data
2. practice : Query the highest salary ratio in each department 10000 High department information
# Wrong writing
SELECT department_id,MAX(salary)
FROM employees
WHERE MAX(salary) > 10000
GROUP BY department_id;
# Write it correctly
SELECT department_id,MAX(salary)
FROM employees
GROUP BY department_id
HAVING MAX(salary) > 10000;
requirement
- If an aggregate function is used in the filter condition , Must be used HAVING To replace WHERE, Otherwise, an error will be reported
- HAVING It must be stated that in GROUP BY Behind
- In development , Use HAVING The premise is SQL Used in GROUP BY
Four 、SQL The underlying execution principle
When there is an aggregate function in the filter condition , Then this filter condition must be declared in HAVING in
When there is no aggregate function in the filter condition , Then this filter condition is declared in WHERE Medium or HAVING Either way , however , It is suggested that you declare in WHERE in
( One )WHERE And HAVING Comparison of
1. Scope of application
HAVING Has a wider scope of application
2. If the filter condition has no aggregate function
WHERE More efficient than HAVING
summary
( Two )SELECT sentence
1.SQL92 The grammar of
SELECT ......( There are aggregate functions )
FROM ......
WHERE Connection conditions of multiple tables AND Filter conditions that do not contain aggregate functions
GROUP BY ......
HAVING Filter conditions containing aggregate functions
ORDER BY ......(ASC / DESC)
LIMIT ......
2.SQL99 The grammar of
SELECT ......( There are aggregate functions )
FROM .... (LEFT / RIGHT) JOIN ... ON Connection conditions of multiple tables
(LEFT / RIGHT) JOIN ... ON ...
WHERE Filter conditions that do not contain aggregate functions
GROUP BY ......
HAVING Filter conditions containing aggregate functions
ORDER BY ......(ASC / DESC)
LIMIT ......
( 3、 ... and )SQL Statement execution
FROM ...... --> ON --> (LEFT / RIGHT JOIN) --> WHERE --> GROUP BY --> HAVING --> SELECT --> DISTINCT( duplicate removal ) --> ORDER BY( Sort ) --> LIMIT( Pagination display )
边栏推荐
- 03 -- QT OpenGL EBO draw triangle
- Luogu-p1107 [bjwc2008] Lei Tao's kitten
- Kubernetes cluster builds efk log collection platform
- The most valuable thing
- 01 - QT OpenGL display OpenGL window
- Using the visualization results, click to appear the corresponding sentence
- Merge K ascending linked lists
- QT -- qfileinfo file information reading
- 第一章:喝汽水,阶梯电费计算,阶梯电费计算函数,个人所税,求解平方根不等式,简化求解平方根不等式,求解调和级数不等式,解不等式:d<1+1/2-1/3+1/4+1/5-1/6+..士1/n
- Day_ 18 IO stream system
猜你喜欢
BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply
第一章:简化同码小数和s(d, n)
01. Preparation for automated office (free guidance, only three steps)
2020 intermediate financial management (escort class)
2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)
[optics] vortex generation based on MATLAB [including Matlab source code 1927]
Chapter 1: find the factorial n of n!
QT -- qfileinfo file information reading
05 -- QT OpenGL draw cube uniform
Day18 - basis of interface testing
随机推荐
Web Security (VIII) what is CSRF attack? Why can token prevent csdf attacks?
I didn't cancel
I study database at station B (4): DQL
Chapter 1: find the factorial n of n!
第一章:喝汽水,阶梯电费计算,阶梯电费计算函数,个人所税,求解平方根不等式,简化求解平方根不等式,求解调和级数不等式,解不等式:d<1+1/2-1/3+1/4+1/5-1/6+..士1/n
BOC protected phenylalanine zinc porphyrin (Zn · TAPP Phe BOC) / iron porphyrin (Fe · TAPP Phe BOC) / nickel porphyrin (Ni · TAPP Phe BOC) / manganese porphyrin (Mn · TAPP Phe BOC) Qiyue Keke
交叉编译Opencv带Contrib
Chapter 1: sum of three factorials, graph point scanning
Use unique_ PTR forward declaration? [repetition] - forward declaration with unique_ ptr? [duplicate]
Flume learning notes
Chapter 1: drinking soft drinks, step tariff calculation, step tariff calculation function, personal income tax, solving square root inequality, simplifying solving square root inequality, solving dem
03 -- QT OpenGL EBO draw triangle
Leetcode 1189. Maximum number of balloons (special character count)
Native table - scroll - merge function
Common text labels
Chapter 1: recursively find the factorial n of n!
math_ Taylor formula
This Chinese numpy quick look-up table is too easy!
CesiumJS 2022^ 源码解读[7] - 3DTiles 的请求、加载处理流程解析
Compared with 4G, what are the advantages of 5g to meet the technical requirements of industry 4.0