Joe Celko's SQL for Smarties: Advanced SQL Programming, Third Edition

Chapter 20: Grouping Operations

I am separating the partitions and grouping operations based on the idea that a group has group properties that we are trying to find, so we get an answer back for each group. A partition is simply a way of subsetting the original table so that we get a table back as a result.

20.1 GROUP BY Clause

The GROUP BY clause is based on simple partitions. A partition of a set divides the set into subsets such that the union of the subsets returns the original set, and the intersection of the subsets is empty. Think of it as cutting up a pizza pie each piece of pepperoni belongs to one and only one slice of pizza. When you get to the section on SQL-99 OLAP extensions, you will see "variations on a theme" in the ROLLUP and CUBE operators, but this is where it all starts.

The GROUP BY clause takes the result of the FROM and WHERE clauses, then puts the rows into groups defined as having the same values for the columns listed in the GROUP BY clause. Each group is reduced to a single row in the result table. This result table is called a grouped table, and all operations are now defined on groups rather than on rows.

By convention, the NULLs are treated as one group. The order of the grouping columns in the

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Industrial Partitions
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.