1. Overview Group by means to group data according to the rules after by. The so-called grouping means dividing the data set into several "small groups" and processing them accordingly. 2. Grammatical rules SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name 3. Examples We have an order table like this: We want to count the total amount of each user's order, we can use group by to achieve this: select Customer,sum(OrderPrice) as total_price group by Customer; The following result set appears: 4. Note If the following appears: select category, sum(quantity) as sum of quantity, summaryfrom Agroup by categoryorder by categorydesc An error will be reported because the selected field must either be included after group by as the basis for grouping, or be included in an aggregate function, otherwise the summary column cannot match a row. select category, sum(quantity) AS sum of quantities from Agroup by category order by sum(quantity) desc If you want to group by multiple columns, you can use the group by all syntax: select Customer,OrderDate,sum(OrderPrice) group by all Customer,OrderDate 5. The difference between where and having where is to remove rows that do not meet the where condition before query grouping, that is, filter data before grouping. The where condition cannot contain aggregate functions. select Customer,sum(OrderPrice) as total_price group by Customer having total_price>1700; The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Restart the Docker service to apply the automatic start and stop command (recommended)
>>: Teach you how to use vscode to build a react-native development environment
1. Create a new configuration file docker_nginx.c...
1. Download the zip archive version from the offi...
Today I received a disk alarm exception. The 50G ...
Modern browsers no longer allow JavaScript to be ...
Table of contents pom configuration Setting.xml c...
I am almost going moldy staying at home due to th...
Promise is a new solution for asynchronous progra...
Solution Add position:relative to the parent elem...
<br />Previous article: Web Design Tutorial ...
1. Best left prefix principle - If multiple colum...
The party that creates a new connection is equiva...
Make an animation of the eight planets in the sol...
Redis Introduction Redis is completely open sourc...
Preface This article mainly introduces the releva...
This article uses a jQuery plug-in to create an a...