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
Using UNION Most SQL queries consist of a single ...
If you want to adjust the size and number of Inno...
Table of contents What is Vuex? Five properties o...
This article example shares the specific code of ...
This article example shares the specific code of ...
This article shares the specific code for the WeC...
In HTML, colors are represented in two ways. One i...
CI/CD Overview CI workflow design Git code versio...
First, understand a method: Entering a Docker con...
This article collects 20 excellent web page color ...
Using the Docker run command docker run -d -p 920...
The database, like the operating system, is a sha...
Table of contents System update configuration Cha...
Table of contents 1. Download 2. Install nginx an...
This article introduces the CSS Sticky Footer imp...