当前位置:网站首页>Summary of SQL aggregate query method for yyds dry goods inventory
Summary of SQL aggregate query method for yyds dry goods inventory
2022-07-01 19:59:00 【51CTO】
SQL Why support aggregate queries ?
This seems like a naive question , But let's think about it step by step . Data is stored at behavioral granularity , The simplest SQL The sentence is select * from test, What you get is the whole two-dimensional table details , But this alone is not enough , For two purposes , need SQL Provide aggregate function :
1. The detailed data has no statistical significance , For example, I want to know the total turnover today , And don't care much about how much a table guest spends .
2. Although you can check the data into memory first and then aggregate , But when the amount of data is very large, it is easy to burst the memory , Maybe a table has a data volume of... In one day 10TB, and 10TB Even if the data can be read into memory , Aggregation computing may also be unacceptably slow .
In addition, aggregation itself has a certain logical complexity , and SQL Provides aggregation functions and grouping aggregation capabilities , It can easily and quickly count the aggregated data with business value , This lays the foundation for SQL The analytical value of language , Therefore, most analysis software directly adopts SQL As a direct user oriented expression .
Aggregate functions
Common aggregate functions are :
- COUNT: Count .
- SUM: Sum up .
- AVG: averaging .
- MAX: For maximum .
- MIN: For the minimum .
COUNT
COUNT Used to calculate how many pieces of data there are , Let's see id How many in this column :
But we found that we actually checked any column COUNT It's all the same , The introduction id What is it ? There is no need to find a specific column to refer to , So it can also be written as :
But there are subtle differences between the two .SQL There is a very special value type NULL, If COUNT Specific columns are specified , This column will be skipped during statistics, and the value is NULL The line of , and COUNT(*) Because no specific column is specified , So even if it includes NULL, Even all columns in a row are NULL, Will also be included . therefore COUNT(*) The result must be greater than or equal to COUNT(c1).
Of course, any aggregate function can follow the query criteria WHERE, such as :
SUM
SUM Sum all terms , Therefore, it must act on the numeric field , Not for strings .
SUM encounter NULL Value when 0 Handle , Because it's equivalent to ignoring .
AVG
AVG Find the value of all terms , Therefore, it must act on the numeric field , Not for strings .
AVG encounter NULL Value is ignored in the most thorough way , namely NULL Completely not involved in the calculation of numerator and denominator , Just like this line of data does not exist .
MAX、MIN
MAX、MIN Find the maximum and minimum values respectively , When the above is different , It can also act on strings , So you can judge the size according to the letters , From big to small a-z, But even if it can be counted , It has no practical significance and is difficult to understand , Therefore, it is not recommended to extremum the string .
Multiple aggregate fields
Although they are aggregate functions , but MAX、MIN Strictly speaking, it is not an aggregate function , Because they just look for lines that meet the conditions . You can see the comparison of the query results in the following two paragraphs :
The first query can find the row with the maximum value id, And the second query id It's meaningless , Because I don't know which line to belong to , So only the first data is returned id.
Of course , If you calculate at the same time MAX、MIN, So at this time id Only the value of the first data is returned , Because the query result corresponds to a complex number of rows :
Based on these characteristics , It's best not to mix polymerization and non polymerization , That is, once a field in a query is aggregated , Then all fields must be aggregated .
A lot now BI All custom fields of the engine have this restriction , Because there are many boundary conditions when mixing aggregation and non aggregation in user-defined memory calculation , although SQL Can support , However, business customized functions may not support .
Group aggregation
Grouping aggregation is GROUP BY, In fact, it can be regarded as an advanced conditional statement .
for instance , Query each country's GDP Total amount :
The returned results are grouped by country , At this time , The aggregation function becomes aggregation within a group .
In fact, if we just want to see 、 beauty GDP, Non grouping can also be used to check , Just divide it into two SQL:
therefore GROUP BY It can also be understood as , Find out all enumerable conditions of a field , And integrate it into a table , Each line represents an enumeration case , It doesn't need to be broken down into one by one WHERE The query .
Multi field grouping aggregation
GROUP BY You can use... For multiple dimensions , The meaning is equivalent to the row in table query / Drag columns into multiple dimensions .
It's on it BI Query tool perspective , If there is no context , You can see the following progressive description :
- Group and aggregate by multiple fields .
- Multiple fields are combined to become unique Key, namely
GROUP BY a,b Express a,b Together, describe a group . -
GROUP BY a,b,c You may see many duplicate results of the first column of the query a That's ok , The second column sees the repetition b That's ok , But in the same a There will be no repetition within the value ,c stay b The same is true in the line .
Here is an example :
GROUP BY + WHERE
WHERE It is filtered according to the criteria of the row . therefore GROUP BY + WHERE Not in groups , It's screening the whole .
But because of filtering by row , In fact, the results are exactly the same within or outside the group , So we can hardly perceive the difference :
However , Ignoring this difference will cause us to run into a wall in aggregation screening .
For example, we should screen out the average score greater than 60 The sum of students' grades , If you don't use subqueries , It cannot be used in ordinary query WHERE Add aggregate function to achieve , For example, the following is an example of a syntax error :
Don't fantasize about the above SQL Can be executed successfully , Not in WHERE Use aggregate functions in .
GROUP BY + HAVING
HAVING It is filtered according to the group . So you can HAVING Using aggregate functions :
In the above example, you can query normally , It means to look at the total score according to the grouping of classes , And only the average score greater than 60 Class .
So why HAVING You can use aggregation conditions ? because HAVING Filtering is a group , Therefore, you can filter out groups that do not meet the conditions after group aggregation , It makes sense . and WHERE For row granularity , After aggregation, there is only one piece of data in the whole table , It doesn't make sense whether it's filtered or not .
But here's the thing ,GROUP BY Index filtering cannot be used to generate derived tables , therefore WHERE You can optimize performance by indexing fields , and HAVING Does not work for index fields .
边栏推荐
猜你喜欢

自定义插入页面标签以及实现类似通讯录的首字母搜索
![[research data] observation on the differences of health preservation concepts among people in 2022 - Download attached](/img/50/926cc5bce83f8b195b3e2072b656bf.jpg)
[research data] observation on the differences of health preservation concepts among people in 2022 - Download attached

【AI服务器搭建】CUDA环境

【无标题】

振弦采集模塊測量振弦傳感器的流程步驟

一个悄然崛起的国产软件,低调又强大!

windows环境 redis安装和启动(后台启动)

由浅入深学会白盒测试用例设计

Optimization of video streaming with repeated requests in the case of unstable easygbs network

Use of common built-in classes of JS
随机推荐
简单但现代的服务器仪表板Dashdot
Redis installation and startup in Windows environment (background startup)
3D全景模型展示可视化技术演示
February 15, 2022: sweeping robot. There is a floor sweeping robot in the room (represented by a grid). Each grid in the grid has two possibilities: empty and obstacles. The sweeping robot provides fo
PowerDesigner design name and comment replacement
使用Zadig从0到1搭建持续交付平台
C # joint halcon Application - Dahua Camera Collection class
DS transunet: Dual Swing transformer u-net for medical image segmentation
uniapp使用腾讯地图选点 没有window监听回传用户的位置信息,怎么处理
windows环境 redis安装和启动(后台启动)
对金额进行求和
Wechat applet realizes keyword highlighting
再回顾集合容器
JVM memory model
Using win7 vulnerability to crack the system login password
Remove line breaks from MySQL query results
tensorflow报错Could not load dynamic library ‘libcudnn.so.8
新增订单如何防止重复提交
Modsim basic use (Modbus simulator)
开发那些事儿:EasyCVR集群设备管理页面功能展示优化