ibm-information-center/dist/eclipse/plugins/i5OS.ic.sqlp_5.4.0.1/rbafygroup.htm

238 lines
12 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="GROUP BY clause" />
<meta name="abstract" content="The GROUP BY clause allows you to find the characteristics of groups of rows rather than individual rows." />
<meta name="description" content="The GROUP BY clause allows you to find the characteristics of groups of rows rather than individual rows." />
<meta name="DC.subject" content="SELECT statement, GROUP BY, example, clause, GROUP BY, SELECT statement, using NULL value with, NULL value, used with GROUP BY clause" />
<meta name="keywords" content="SELECT statement, GROUP BY, example, clause, GROUP BY, SELECT statement, using NULL value with, NULL value, used with GROUP BY clause" />
<meta name="DC.Relation" scheme="URI" content="rbafytexas.htm" />
<meta name="DC.Relation" scheme="URI" content="rbafyussisql.htm" />
<meta name="DC.Relation" scheme="URI" content="rbafyorderby.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="rbafygroup" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>GROUP BY clause</title>
</head>
<body id="rbafygroup"><a name="rbafygroup"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">GROUP BY clause</h1>
<div><p>The GROUP BY clause allows you to find the characteristics of groups
of rows rather than individual rows.</p>
<div class="section"><p>When you specify a GROUP BY clause, SQL divides the selected rows
into groups such that the rows of each group have matching values in one or
more columns or expressions. Next, SQL processes each group to produce a single-row
result for the group. You can specify one or more columns or expressions
in the GROUP BY clause to group the rows. The items you specify in the SELECT
statement are properties of each group of rows, not properties of individual
rows in a table or view.</p>
<p>Without a GROUP BY clause, the application
of SQL aggregate functions returns <em>one</em> row. When GROUP BY is used,
the function is applied to <em>each</em> group, thereby returning
as many rows as there are groups.</p>
</div>
<div class="section"><p>For example, the CORPDATA.EMPLOYEE table has several sets of rows,
and each set consists of rows describing members of a specific department.
To find the average salary of people in each department, you can issue:</p>
</div>
<div class="section"><pre><strong>SELECT</strong> WORKDEPT, <strong>DECIMAL</strong> (<strong>AVG</strong>(SALARY),5,0)
<strong>FROM</strong> CORPDATA.EMPLOYEE
<strong>GROUP BY</strong> WORKDEPT</pre>
</div>
<div class="section"><p>The result is several rows, one for each department.</p>
</div>
<div class="section">
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" frame="hsides" border="1" rules="all"><thead align="left"><tr><th valign="top" id="d0e87">WORKDEPT</th>
<th valign="top" id="d0e89">AVG-SALARY</th>
</tr>
</thead>
<tbody><tr><td valign="top" headers="d0e87 ">A00</td>
<td valign="top" headers="d0e89 ">40850</td>
</tr>
<tr><td valign="top" headers="d0e87 ">B01</td>
<td valign="top" headers="d0e89 ">41250</td>
</tr>
<tr><td valign="top" headers="d0e87 ">C01</td>
<td valign="top" headers="d0e89 ">29722</td>
</tr>
<tr><td valign="top" headers="d0e87 ">D11</td>
<td valign="top" headers="d0e89 ">25147</td>
</tr>
<tr><td valign="top" headers="d0e87 ">D21</td>
<td valign="top" headers="d0e89 ">25668</td>
</tr>
<tr><td valign="top" headers="d0e87 ">E01</td>
<td valign="top" headers="d0e89 ">40175</td>
</tr>
<tr><td valign="top" headers="d0e87 ">E11</td>
<td valign="top" headers="d0e89 ">21020</td>
</tr>
<tr><td valign="top" headers="d0e87 ">E21</td>
<td valign="top" headers="d0e89 ">24086</td>
</tr>
</tbody>
</table>
</div>
<div class="note"><span class="notetitle">Notes:</span> <ol><li>Grouping the rows does not mean ordering them. Grouping puts each selected
row in a group, which SQL then processes to derive characteristics of the
group. Ordering the rows puts all the rows in the results table in ascending
or descending collating sequence. Depending
on the implementation selected by the database manager, the resulting groups
may appear to be ordered.</li>
<li>If there are null values in the column you specify in the GROUP BY clause,
a single-row result is produced for the data in the rows with null values.</li>
<li>If the grouping occurs over character, or UCS-2 or UTF-16 graphic columns,
the sort sequence in effect when the query is run is applied to the grouping. </li>
</ol>
</div>
</div>
<div class="section"><p>When you use GROUP BY, you list the columns or expressions you
want SQL to use to group the rows. For example, suppose you want a list of
the number of people working on each major project described in the CORPDATA.PROJECT
table. You can issue:</p>
</div>
<div class="section"><pre><strong>SELECT</strong> <strong>SUM</strong>(PRSTAFF), MAJPROJ
<strong> FROM</strong> CORPDATA.PROJECT
<strong> GROUP BY</strong> MAJPROJ</pre>
</div>
<div class="section"><p>The result is a list of the company's current major projects and
the number of people working on each project: </p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" frame="hsides" border="1" rules="all"><thead align="left"><tr><th valign="top" id="d0e167">SUM(PRSTAFF)</th>
<th valign="top" id="d0e169">MAJPROJ</th>
</tr>
</thead>
<tbody><tr><td valign="top" headers="d0e167 ">6</td>
<td valign="top" headers="d0e169 ">AD3100</td>
</tr>
<tr><td valign="top" headers="d0e167 ">5</td>
<td valign="top" headers="d0e169 ">AD3110</td>
</tr>
<tr><td valign="top" headers="d0e167 ">10</td>
<td valign="top" headers="d0e169 ">MA2100</td>
</tr>
<tr><td valign="top" headers="d0e167 ">8</td>
<td valign="top" headers="d0e169 ">MA2110</td>
</tr>
<tr><td valign="top" headers="d0e167 ">5</td>
<td valign="top" headers="d0e169 ">OP1000</td>
</tr>
<tr><td valign="top" headers="d0e167 ">4</td>
<td valign="top" headers="d0e169 ">OP2000</td>
</tr>
<tr><td valign="top" headers="d0e167 ">3</td>
<td valign="top" headers="d0e169 ">OP2010</td>
</tr>
<tr><td valign="top" headers="d0e167 ">32.5</td>
<td valign="top" headers="d0e169 ">?</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section"><p>You can also specify that you want the rows grouped by more than
one column or expression. For example, you can issue a select-statement to
find the average salary for men and women in each department, using the CORPDATA.EMPLOYEE
table. To do this, you can issue:</p>
</div>
<div class="section"><pre><strong>SELECT</strong> WORKDEPT, SEX, <strong>DECIMAL</strong>(<strong>AVG</strong>(SALARY),5,0) <strong>AS</strong> AVG_WAGES
<strong>FROM</strong> CORPDATA.EMPLOYEE
<strong>GROUP BY</strong> WORKDEPT, SEX</pre>
</div>
<div class="section"><p>Results in: </p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" frame="hsides" border="1" rules="all"><thead align="left"><tr><th valign="top" width="33.33333333333333%" id="d0e245">WORKDEPT</th>
<th valign="top" width="33.33333333333333%" id="d0e247">SEX</th>
<th valign="top" width="33.33333333333333%" id="d0e249">AVG_WAGES</th>
</tr>
</thead>
<tbody><tr><td valign="top" width="33.33333333333333%" headers="d0e245 ">A00</td>
<td valign="top" width="33.33333333333333%" headers="d0e247 ">F</td>
<td valign="top" width="33.33333333333333%" headers="d0e249 ">49625</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e245 ">A00</td>
<td valign="top" width="33.33333333333333%" headers="d0e247 ">M</td>
<td valign="top" width="33.33333333333333%" headers="d0e249 ">35000</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e245 ">B01</td>
<td valign="top" width="33.33333333333333%" headers="d0e247 ">M</td>
<td valign="top" width="33.33333333333333%" headers="d0e249 ">41250</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e245 ">C01</td>
<td valign="top" width="33.33333333333333%" headers="d0e247 ">F</td>
<td valign="top" width="33.33333333333333%" headers="d0e249 ">29722</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e245 ">D11</td>
<td valign="top" width="33.33333333333333%" headers="d0e247 ">F</td>
<td valign="top" width="33.33333333333333%" headers="d0e249 ">25817</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e245 ">D11</td>
<td valign="top" width="33.33333333333333%" headers="d0e247 ">M</td>
<td valign="top" width="33.33333333333333%" headers="d0e249 ">24764</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e245 ">D21</td>
<td valign="top" width="33.33333333333333%" headers="d0e247 ">F</td>
<td valign="top" width="33.33333333333333%" headers="d0e249 ">26933</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e245 ">D21</td>
<td valign="top" width="33.33333333333333%" headers="d0e247 ">M</td>
<td valign="top" width="33.33333333333333%" headers="d0e249 ">24720</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e245 ">E01</td>
<td valign="top" width="33.33333333333333%" headers="d0e247 ">M</td>
<td valign="top" width="33.33333333333333%" headers="d0e249 ">40175</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e245 ">E11</td>
<td valign="top" width="33.33333333333333%" headers="d0e247 ">F</td>
<td valign="top" width="33.33333333333333%" headers="d0e249 ">22810</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e245 ">E11</td>
<td valign="top" width="33.33333333333333%" headers="d0e247 ">M</td>
<td valign="top" width="33.33333333333333%" headers="d0e249 ">16545</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e245 ">E21</td>
<td valign="top" width="33.33333333333333%" headers="d0e247 ">F</td>
<td valign="top" width="33.33333333333333%" headers="d0e249 ">25370</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e245 ">E21</td>
<td valign="top" width="33.33333333333333%" headers="d0e247 ">M</td>
<td valign="top" width="33.33333333333333%" headers="d0e249 ">23830</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section"><p>Because you did not include a WHERE clause in this example, SQL
examines and process all rows in the CORPDATA.EMPLOYEE table. The rows are
grouped first by department number and next (within each department) by sex
before SQL derives the average SALARY value for each group.</p>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafytexas.htm" title="Learn a variety of ways of tailoring your query to gather data using the SELECT statement. One way to do this is to use the SELECT statement in a program to retrieve a specific row (for example, the row for an employee). Furthermore, you can use clauses to gather data in a specific way.">Retrieve data using the SELECT statement</a></div>
</div>
<div class="relconcepts"><strong>Related concepts</strong><br />
<div><a href="rbafyussisql.htm" title="A sort sequence defines how characters in a character set relate to each other when they are compared or ordered. Normalization allows you to compare strings that contain combining characters.">Sort sequences and normalization in SQL</a></div>
</div>
<div class="relref"><strong>Related reference</strong><br />
<div><a href="rbafyorderby.htm" title="The ORDER BY clause specifies the particular order in which you want selected rows returned. The order is sorted by ascending or descending collating sequence of a column's or expression's value.">ORDER BY clause</a></div>
</div>
</div>
</body>
</html>