当前位置:网站首页>100 important knowledge points that SQL must master: grouping data
100 important knowledge points that SQL must master: grouping data
2022-06-30 11:02:00 【Guge academic】
10.1 The data packet
I learned from the last lesson , Use SQL Aggregate functions can aggregate data . such , We can
Count rows , Calculate the sum and average , Get the maximum and minimum values without retrieving all the data
Small value .
All calculations so far have been based on all data in the table or matching a specific WHERE Number of clauses
According to the above . For example, the following example returns the supplier DLL01 Number of products offered :
Input ▼
SELECT COUNT(*) AS num_prods
FROM Products
WHERE vend_id = 'DLL01';
Output ▼
num_prods
-----------
4
If you want to return the number of products provided by each supplier , What should I do ? Or return with only one
The product of the supplier of the product , Or return to provide 10 Products from suppliers of more than products ,
What do I do ?
This is the time for groups to show their skills . Using grouping, you can divide data into multiple logical groups ,
Aggregate each group .
10.2 Create grouping
Grouping is using SELECT Of the statement GROUP BY Clause . The best way to understand grouping
Look at an example :
Input ▼
SELECT vend_id, COUNT(*) AS num_prods
FROM Products
GROUP BY vend_id;
Output ▼
vend_id num_prods
------- ---------
BRS01 3
DLL01 4
FNG01 2
analysis ▼
above SELECT Statement specifies two columns : vend_id Include product suppliers ID,
num_prods For the calculated field ( use COUNT(*) Function creation ). GROUP BY Clause indicates
DBMS Press vend_id Sort and group data . This will be for everyone vend_id Not the whole thing
Table calculation num_prods once . As you can see from the output , supplier BRS01 Yes 3 Individual property
product , supplier DLL01 Yes 4 A product , And suppliers FNG01 Yes 2 A product .
Because of the use of GROUP BY , It is not necessary to specify each group to be calculated and valued . The system will automatically
It's done . GROUP BY Clause indicates DBMS Grouped data , Then for each group instead of the whole
Result sets are aggregated .
In the use of GROUP BY Before clause , You need to know some important rules .
GROUP BY Clause can contain any number of columns , Thus, groups can be nested ,
More detailed data grouping .
If in GROUP BY Group nested in Clause , The data will advance on the last specified group
Row summary . let me put it another way , When creating a group , All columns specified are calculated together ( therefore
Data cannot be retrieved from individual columns ).
GROUP BY Each column listed in the clause must be a retrieval column or a valid expression ( but
Cannot be an aggregate function ). If in SELECT Using expressions in , Must be in GROUP BY
Clause specifies the same expression . Cannot use alias .
majority SQL Implementation does not allow GROUP BY Columns have variable length data types ( Such as Wen
This or remark type field ).
Except for aggregate calculation statements , SELECT Each column in the statement must be in GROUP BY Clause
Give in .
If the grouping column contains NULL Row of values , be NULL Will be returned as a group .
If there are more than one row in the column NULL value , They will be divided into groups .
GROUP BY Clause must appear in WHERE After Clause , ORDER BY Before clause .
Tips : ALL Clause
Microsoft SQL Server Wait some SQL Realize in GROUP BY Optional... Is supported in ALL
Clause . This clause can be used to return all groups , Even groups that do not have matching rows return
return ( In this case , The aggregation will return NULL ). Concrete DBMS Do you support ALL ,
Please refer to the corresponding documentation .
Be careful : Specify columns by relative position
yes , we have SQL The implementation allows you to SELECT The position in the list specifies GROUP BY The column of .
for example , GROUP BY 2, 1 Can represent grouping by the second column selected , Then press the first
Columns grouped . Although this shorthand grammar is very convenient , But not all SQL The implementation supports ,
And it's easy to edit SQL Error in statement
10.3 Filter grouping
Apart from being able to use GROUP BY Out of packet data ,SQL It also allows filtering groups , What does the regulation include
grouping , Which groups are excluded . for example , You may want to list all customers who have at least two orders
customer . So , Filtering must be based on complete grouping rather than individual rows .
We have seen WHERE The function of clause ( The first 4 Class mention ). however , In this example
in WHERE Unable to complete the task , because WHERE Filter specifies rows rather than groups . The facts
On , WHERE No concept of grouping .
that , Don't use WHERE What to use ?SQL Another clause is provided for this purpose , Namely
HAVING Clause . HAVING Very similar to WHERE . in fact , What I have learned so far
All types of WHERE Clause can be used HAVING To replace . The only difference is , WHERE
Filter line , and HAVING Filter grouping .
Tips : HAVING Support all WHERE The operator
In the 4 Lesson and lesson 5 In class , We learned WHERE Condition of clause ( Including wildcard bar
And clauses with multiple operators ). What I've learned is about WHERE All technologies and options
Items apply to HAVING . Their syntax is the same , Only the key words are different .
that , How to filter groups ? Look at the following example :
Input ▼
SELECT cust_id, COUNT(*) AS orders
FROM Orders
GROUP BY cust_id
HAVING COUNT(*) >= 2;
Output ▼
cust_id orders
---------- -----------
1000000001 2
analysis ▼
This article SELECT The first three lines of the statement are similar to the above statement . The last line adds HAVING
Clause , It filters COUNT(*) >= 2 ( More than two orders ) Those groupings .
You can see , WHERE Clause doesn't work here , Because filtering is based on grouping aggregation values ,
Not the value of a particular row .
explain : HAVING and WHERE The difference between
Here is another way to understand , WHERE Filter before data grouping , HAVING In number
Filter after grouping . This is an important difference , WHERE Excluded lines are not included in
In group . This may change the calculated value , Thereby affecting HAVING Clause based on these values
Filtered groups .
that , Have you used... In one statement at the same time WHERE and HAVING The need for clauses ? The facts
On , It does . If you want to further filter the above statements , Return it to the past 12 Within months
Customers with more than two orders . So , You can add one WHERE Clause , Filter out the past 12 Months
Orders placed within , And then add HAVING Clause to filter out groups with more than two orders .
Just to understand , Let's look at the following example , It lists two or more products and their prices
Greater than or equal to 4 Supplier :
Input ▼
SELECT vend_id, COUNT(*) AS num_prods
FROM Products
WHERE prod_price >= 4
GROUP BY vend_id
HAVING COUNT(*) >= 2;
Output ▼
vend_id num_prods
------- -----------
BRS01 3
FNG01 2
analysis ▼
In this statement , The first line is the basic... Using the aggregation function SELECT sentence , It's very similar to the previous one
Example . WHERE Clause filter all prod_price At least for 4 The line of , Then press vend_id
Grouped data , HAVING Clause filter count is 2 or 2 Groups above . without WHERE
Clause , One more line will be retrieved ( supplier DLL01 , sales 4 A product , The prices are all in 4
following ):
Input ▼
SELECT vend_id, COUNT(*) AS num_prods
FROM Products
GROUP BY vend_id
HAVING COUNT(*) >= 2;
Output ▼
vend_id num_prods
------- -----------
BRS01 3
DLL01 4
FNG01 2
explain : Use HAVING and WHERE
HAVING And WHERE Very similar , If you don't specify GROUP BY , Most of them DBMS
Will treat them equally . however , You have to be able to distinguish this for yourself . Use HAVING Should be
The combination GROUP BY Clause , and WHERE Clause is used for standard row level filtering .
10.4 Grouping and sorting
GROUP BY and ORDER BY Do the same job often , But they are very different , Understand this
This is very important . surface 10-1 Sum up the differences between them .
surface 10-1 ORDER BY And GROUP BY
ORDER BY GROUP BY
Sort the resulting output Group lines , But the output may not be in the order of grouping
Any column can use ( Not even
The selected column can also use )
Only selection columns or expression columns can be used , And you must use each selection column
expression
Not necessarily If you use columns with aggregate functions ( Or expressions ), Must be used
surface 10-1 The first difference listed in is extremely important . We often find , use GROUP BY grouping
The data of is indeed output in grouping order . But not always , This is not SQL Specifications
Required . Besides , Even if a particular DBMS Always follow the given GROUP BY Number of clause sorts
According to the , Users may also require sorting in different order . Just because you group numbers in some way
According to the ( Get a specific group aggregation value ), That doesn't mean you need to sort the output the same way .
Should provide clear ORDER BY Clause , Even if the effect is equivalent to GROUP BY Clause .
Tips : Don't forget it ORDER BY
Generally in use GROUP BY When clause , It should also be given ORDER BY Clause . This is Bao
The only way to verify that the data is sorted correctly . Don't just rely on GROUP BY Sorting data .
To illustrate GROUP BY and ORDER BY How to use , Let's look at an example . Below SELECT
The statement is similar to the previous examples . It retrieves order numbers and orders that contain three or more items
The number of items :
Input ▼
SELECT order_num, COUNT(*) AS items
FROM OrderItems
GROUP BY order_num
HAVING COUNT(*) >= 3;
Output ▼
order_num items
--------- -----
20006 3
20007 5
20008 5
20009 3
To sort the output by the number of items ordered , Need to add ORDER BY Clause , As shown below :
Input ▼
SELECT order_num, COUNT(*) AS items
FROM OrderItems
GROUP BY order_num
HAVING COUNT(*) >= 3
ORDER BY items, order_num;
Output ▼
order_num items
--------- -----
20006 3
20009 3
20007 5
20008 5
analysis ▼
In this case , Use GROUP BY Clause by order number ( order_num Column ) Number of groups
According to the , In order to COUNT(*) Function can return the number of items in each order . HAVING Clause
Filtering data , So that only orders containing three or more items are returned . Last , use ORDER BY
Clause sort output .
10.5 SELECT Clause order
Let's review SELECT The order of sentences in a sentence . surface 10-2 In the SELECT In the sentence
The order that must be followed when using , List the clauses learned so far .
surface 10-2 SELECT Clauses and their order
Son sentence say bright Whether it is necessary to use
SELECT
Column or expression to return yes
FROM
A table from which to retrieve data Use only when selecting data from a table
WHERE
Row level filtering no
GROUP BY
Group description Use only when aggregating by group
HAVING
Group level filtering no
ORDER BY
Output sort order no
边栏推荐
- The number of users of the home-made self-developed system exceeded 400million, breaking the monopoly of American enterprises, and Google repented
- 【无标题】
- [STL source code analysis] iterator
- 【Proteus仿真】Arduino UNO LED模拟交通灯
- Qt之实现QQ天气预报窗体翻转效果
- [untitled]
- The first China Digital Collection conference will be held soon
- 【leetcode 239】滑动窗口
- LeetCode Algorithm 86. 分隔链表
- iptables目标TPROXY
猜你喜欢

Q-Learning笔记

20万奖金池!【阿里安全 × ICDM 2022】大规模电商图上的风险商品检测赛火热报名中!...

Deep dive kotlin synergy (18): hot and cold data flow

Deep dive kotlin synergy (16): Channel

Cp2112 teaching example of using USB to IIC communication

Jetpack Compose DropdownMenu跟随手指点击位置显示

OLAP数据库引擎如何选型?

Time complexity and space complexity

ArrayList and sequence table

The first China Digital Collection conference will be held soon
随机推荐
[rust daily] the first rust monthly magazine on January 22, 2021 invites everyone to participate
[STL source code analysis] iterator
Wireguard simple configuration
Mysql database foundation: TCL transaction control language
SGD has many improved forms. Why do most papers still use SGD?
Cp2112 teaching example of using USB to IIC communication
Ant financial's written test question: what can be quantified in the requirements document? [Hangzhou multi tester] [Hangzhou multi tester \wang Sir]
List介绍
China will force a unified charging interface. If Apple does not bow its head, iPhone will be kicked out of the Chinese market
How can the sports app keep the end-to-side background alive to make the sports record more complete?
Iptables target tproxy
蚂蚁金服笔试题:需求文档有什么可以量化的【杭州多测师】【杭州多测师_王sir】...
国产自研系统的用户突破4亿,打破美国企业的垄断,谷歌后悔不迭
Machine learning interview preparation (I) KNN
中国将强制统一充电接口,苹果如不低头,iPhone将被踢出中国市场
20万奖金池!【阿里安全 × ICDM 2022】大规模电商图上的风险商品检测赛火热报名中!...
高通发布物联网案例集 “魔镜”、数字农业已经成为现实
Qt之实现QQ天气预报窗体翻转效果
pytorch 筆記 torch.nn.BatchNorm1d
【STL源码剖析】容器(待补充)