Stairway to T-SQL DML Level 7

write picture description here
This article in the series is part of the Ladder series: Ladder to T-SQL DML.
Using the Transact-SQL (T-SQL) dialect of SQL Server, this ladder will provide you with a basic understanding of how to work with SQL Server table data. DML is a data processing language, an aspect of the language that deals with data. It includes statement select, insert, update and delete. This ladder will provide some history of the SQL language and some general concepts about set theory. Each level will build on the previous level, so when you're done, you'll have a good understanding of how to select and modify data from SQL Server.
In Ladder 6, I showed you how to use the ORDER BY clause to sort data. This allows you to sort detailed records based on single or multiple columns. Detailed data is great if you want to see data in a specific record, but sometimes you need to aggregate detailed data into aggregated values. Summarize what you can do with the GROUP BY clause.
There are two types of GROUP BY clauses. One is called a simple GROUP BY clause, and the other provides a simple generalization, called a general GROUP BY clause. The main difference between these two types is that simple GROUP BY contains only GROUP BY clause, while general GROUP BY clause contains other operators such as ROLLUP and CUBE.
In this article, I will describe how to group data using a simple group BY clause. In a follow-up article, I will discuss the more complex general GROUP BY clause.
Simple GROUP BY clause
Use the simple GROUP BY clause to aggregate data based on a single column, multiple columns, or expressions. Returns only one summary row for each unique value based on the columns and/or expressions specified in the GROUP BY clause. When SQL Server processes a group by clause, it groups the detail records by unique column or expression values, and then summarizes each collection based on the aggregate functions contained in the select list.
To better understand how to use GROUP BY, let's assume that you have a table with detailed sales information for different stores, and you want to store the total sales. You can use the GROUP BY clause to aggregate the total sales for each store. In this example, the only column you will group by will be store name, and the column you will aggregate will be sales. Your results will show one row for each unique store name, and each store's row will contain the sum of sales for that store.
By querying, SQL Server has some restrictions on which columns can be included in a group's select list. Each column specified in the select list of the query group needs to fall into one of the following categories:
• Column specified in the GROUP BY clause.
• Expressions specified in the GROUP BY clause.
• Values ​​returned from aggregate functions.
If a column doesn't fall into one of these categories, you'll get an error when you try to run your group through the query. Note that columns or expressions included in the GROUP BY clause do not need to be in the select list.
Let me walk through a few examples to help demonstrate how to use the simple GROUP BY clause to get aggregated values.
Example data for a simple GROUP BY clause.
To demonstrate how to use a simple GROUP BY clause, I need to build some sample data. I've provided a script to create my sample data so you can run the sample code provided with this article. Use the script in Listing 1 to build and populate the example table. write picture description herewrite picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324647973&siteId=291194637