t-sql DML level 7: Aggregate data using simple group by clause

Using expressions in the GROUP BY clause

Sometimes you may want to group data based on something other than a specific column or set. For example, you might want to summarize data based on the first few characters of some VARCHAR column, or just the date or month of a DateTime column . SQL Server allows you to specify expressions in the GROUP BY clause to accomplish this task. The expression can be any valid expression based on a column in the detail recordset being aggregated. To demonstrate how to use expressions in the GROUPBY clause, look at the code in Listing 4.

 

Listing 4: Grouping by a single column in Listing 4

The SELECT statement groups data based on an expression, in this case a transformation function. If an expression is used in the GROUPBY clause, the same expression must be used in the SELECT list. The conversion function parses the SalesDateTime column and returns only the date portion of the column. Using the transform function in the GROUP BY clause, I can summarize the sales data based on the actual dates of the different sales records. By doing this, I was able to summarize my example to get the TotalSalesAmount for all stores by date as shown in report 3.

 

Report 3: When using expressions to summarize expression-based data

When using expressions to summarize expression-based data, it allows you to programmatically identify which parts of the detailed data will be used to aggregate the data.

Terms of use

If you are using the GROUPBY clause to aggregate data, you may not want to return all aggregated values. Instead, you may only want to return a subset of the aggregated data. The HAVE clause can be used to optionally identify aggregated values ​​by summarizing.

Usually, when we select data, we use the WHERE clause to limit the rows returned. The only problem is that the WHERE clause operates on row values, not aggregated values. Therefore, the WHERE clause cannot use aggregated values ​​created by the GROUP BY clause. However, adding the HAVING clause after the GROUP BY clause gives you a way to specify conditions to identify specific aggregated values ​​to return. To understand this better, let me give you a few examples.

When viewing store sales data, the HAVING clause may be used to identify stores that do not meet a certain sales quota. If you wanted to find all the stores that didn't meet the minimum sales amount, you could use the code in Listing 5 to do so.

 

Listing 5: Limiting the result set with the HAVING clause

By using the HITH clause in Listing 5 to limit the result set, I limited the results to stores with aggregated TotalSalesAmount less than 1000.00. In my simple example here, you'll find that the StoreName for "Computer Books and Software" is the only store that doesn't reach the $1000.00 sales limit. The HAVING clause can be used on columns that are also not aggregated. You can also do this if you want to limit the returned rows based on specific values ​​of any of the columns used in the GROUPBY clause, as Listing 6 demonstrates.

 

Listing 6: Restricting the result set by column by column in Listing 6

I only want to see aggregated data for stores that have "Outlet" or "Books" in the store name -- this example also demonstrates that there can be multiple conditions in the HANING clause. Another way to think about the difference between WHERE and WITH is that the WHERE clause filters the data rows before the data is aggregated, while the HAVING clause filters the aggregated rows after the GROUP BY is applied. Summarize data by clause with simple groups

在本文中,我向您展示了如何使用简单的GROUP BY子句来总结数据。我讨论了如何使用单个列、多列以及GROUP BY子句中的表达式来总结详细数据。通过使用我演示的内容,您现在应该能够构建一个简单的GROUPBY子句来总结数据,并且可以选择地使用已有的。在我的下一篇文章中,我将扩展我对GROUP BY子句的讨论。在这篇后续文章中,我将向您展示如何使用多维数据集和汇总操作符来生成额外的汇总数据,如小计值和总计值。

 

 

Guess you like

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