Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

GROUP BY in SQL query

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces GROUP BY in SQL query, which is very detailed and has certain reference value. Friends who are interested must finish reading it!

What does GROUP BY mean in a SQL query?

In SQL query, GROUP BY means to group data according to the specified rules, and its function is to group query results according to the values of one or more columns, and those with equal values are a group. GROUP BY statements are generally used in conjunction with aggregate functions.

Group By and Order By

Select category, sum (quantity) sum of AS quantity sum from A group by category order by sum (quantity) desc

Sum of order by desc cannot be used in Access, but can be used in SQL Server.

Field limits specified by Select in Group By

Select category, sum (quantity) the sum of as quantity, summary from A group by category order by category desc

An error will be prompted after execution, as shown in the following figure. This is the point to note that the fields specified by select are either included at the end of the Group By statement as the basis for grouping or are included in the aggregate function.

Group By All

Select category, summary, sum (quantity) sum of as quantity from A group by all category, summary

The summary field can be specified in because the summary field is included in the multi-column grouping, and the execution results are as follows

"Multi-column grouping" is actually grouped according to the merged values of multiple columns (category + summary). In example 4, you can see that "a, a 2001, 13" is the merge of "a, a 2001, 11" and "a, a 2001, 2".

Although "group by all" is supported in SQL Server, GROUP BY ALL will be removed in future versions of Microsoft SQL Server to avoid using GROUP BY ALL in new development work. "Group By All" is not supported in Access, but multi-column grouping is also supported in Access. The SQL in the above SQL Server can be written as

Select category, summary, sum (quantity) sum of AS quantity from A group by category, summary

The above is all the contents of the article "GROUP BY in SQL query". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report