CHAPTER 5

image

Grouping and Summarizing

by Wayne Sheffield

Grouping is primarily performed in SQL Server by using the GROUP BY clause in a SELECT query to determine in which groups rows should be put. Data is summarized by using the SUM function. The simplified syntax is as follows:

SELECT Column1, SUM(Column2)FROM table_list[WHERE search_conditions]GROUP BY Column1

GROUP BY follows the optional WHERE clause and is most often used when aggregate functions are being utilized in the SELECT statement (aggregate functions are reviewed in more detail in Chapter 7).

5-1. Summarizing a Result Set

Problem

You need to know the total number of items in your ...

Get SQL Server 2012 T-SQL Recipes: A Problem-Solution Approach now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.