GROUP BY

<group_by_clause> ::= GROUP BY <field_expr_list> [ WITH ROLLUP ]
<field_expr_list> ::= <field_expr> [ , <field_expr> ... ]

The GROUP BY clause specifies the groups into which output rows are to be placed and, if aggregate functions are included in the SELECT or HAVING clauses, calculates the aggregate functions values for each group.


Arguments:

WITH ROLLUP

Specifies that in addition to the usual rows provided by GROUP BY, summary rows are introduced into the result set. Groups are summarized in a hierarchical order, from the lowest level in the group to the highest, and the corresponding summary rows contain NULL values for the groups that have been summarized.
The group hierarchy is determined by the order in which the grouping field-expressions are specified. Changing the order of the grouping field-expressions can affect the number of rows produced in the result set.
The ROLLUP operator is often used with the GROUPING aggregate function.

Remarks:


Examples:

A. Simple GROUP BY clause

The following query, on an IISW3C log file, returns the number of requests for each page on each day:
SELECT date, cs-uri-stem, COUNT(*)
FROM LogFiles\ex040528.log
GROUP BY date, cs-uri-stem
A sample output would be:
date	 cs-uri-stem		 COUNT(ALL *)
---------- ------------------- ------------
2003-11-18 /Default.htm		1
2003-11-18 /style.css		1
2003-11-18 /images/address.gif 1
2003-11-18 /cgi-bin/counts.exe 1
2003-11-18 /data/rulesinfo.nsf 2
2003-11-19 /data/rulesinfo.nsf 6
2003-11-20 /data/rulesinfo.nsf 5
2003-11-20 /maindefault.htm	1
2003-11-20 /top2.htm		 1
2003-11-20 /homelog.swf		1

B. Using WITH ROLLUP

The following example query is the same as in the previous example, using the WITH ROLLUP argument to display additional summary rows:
SELECT date, cs-uri-stem, COUNT(*)
FROM LogFiles\ex040528.log
GROUP BY date, cs-uri-stem WITH ROLLUP
A sample output would be:
date	 cs-uri-stem		 COUNT(ALL *)
---------- ------------------- ------------
2003-11-18 /Default.htm		1
2003-11-18 /style.css		1
2003-11-18 /images/address.gif 1
2003-11-18 /cgi-bin/counts.exe 1
2003-11-18 /data/rulesinfo.nsf 2
2003-11-19 /data/rulesinfo.nsf 6
2003-11-20 /data/rulesinfo.nsf 5
2003-11-20 /maindefault.htm	1
2003-11-20 /top2.htm		 1
2003-11-20 /homelog.swf		1
-		-				 20
2003-11-18 -				 6
2003-11-19 -				 6
2003-11-20 -				 8
The group summaries that have been introduced by the rollup operator are:
2003-11-18 -				 6
2003-11-19 -				 6
2003-11-20 -				 8
-		-				 20
Which represent the number of requests on each day, regardless of the page requested, and the total number of requests in the log file, regardless of the day.

See also:

Field Expressions
SELECT

Aggregating Data Within Groups


© 2004 Microsoft Corporation. All rights reserved.