90 lines
5.6 KiB
HTML
90 lines
5.6 KiB
HTML
<?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="Handle duplicate rows" />
|
|
<meta name="abstract" content="When SQL evaluates a select-statement, several rows might qualify to be in the result table, depending on the number of rows that satisfy the select-statement's search condition. Some of the rows in the result table might be duplicates." />
|
|
<meta name="description" content="When SQL evaluates a select-statement, several rows might qualify to be in the result table, depending on the number of rows that satisfy the select-statement's search condition. Some of the rows in the result table might be duplicates." />
|
|
<meta name="DC.subject" content="SELECT statement, preventing duplicate rows, statements, SELECT, row, preventing duplicate, examples, preventing duplicate rows, DISTINCT keyword, keyword, DISTINCT" />
|
|
<meta name="keywords" content="SELECT statement, preventing duplicate rows, statements, SELECT, row, preventing duplicate, examples, preventing duplicate rows, DISTINCT keyword, keyword, DISTINCT" />
|
|
<meta name="DC.Relation" scheme="URI" content="rbafytexas.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="rbafyussisql.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="rbafydupl" />
|
|
<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>Handle duplicate rows</title>
|
|
</head>
|
|
<body id="rbafydupl"><a name="rbafydupl"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Handle duplicate rows</h1>
|
|
<div><p>When SQL evaluates a select-statement, several rows might qualify
|
|
to be in the result table, depending on the number of rows that satisfy the
|
|
select-statement's search condition. Some of the rows in the result table
|
|
might be duplicates.</p>
|
|
<div class="section"><p>You can specify that you do not want any duplicates by using the
|
|
DISTINCT keyword, followed by the list of expressions: </p>
|
|
<pre><strong>SELECT DISTINCT</strong> JOB, SEX
|
|
…</pre>
|
|
</div>
|
|
<div class="section"><p>DISTINCT means you want to select only the unique rows. If a selected
|
|
row duplicates another row in the result table, the duplicate row is ignored
|
|
(it is not put into the result table). For example, suppose you want a list
|
|
of employee job codes. You do not need to know which employee has what job
|
|
code. Because it is probable that several people in a department have the
|
|
same job code, you can use DISTINCT to ensure that the result table has only
|
|
unique values.</p>
|
|
</div>
|
|
<div class="section"><p>The following example shows how to do this:</p>
|
|
<pre> <strong>SELECT DISTINCT</strong> JOB
|
|
<strong>FROM</strong> CORPDATA.EMPLOYEE
|
|
<strong>WHERE</strong> WORKDEPT = 'D11'</pre>
|
|
</div>
|
|
<div class="section"><p>The result is two rows:</p>
|
|
|
|
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" frame="hsides" border="1" rules="all"><thead align="left"><tr valign="top"><th valign="top" id="d0e74">JOB</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody><tr><td valign="top" headers="d0e74 ">DESIGNER</td>
|
|
</tr>
|
|
<tr><td valign="top" headers="d0e74 ">MANAGER</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
<div class="section"><p>If you do not include DISTINCT in a SELECT clause, you might find
|
|
duplicate rows in your result, because SQL returns the <em>JOB</em> column's
|
|
value for each row that satisfies the search condition. Null values are treated
|
|
as duplicate rows for DISTINCT.</p>
|
|
</div>
|
|
<div class="section"><p>If you include DISTINCT in a SELECT clause and you also include
|
|
a shared-weight sort sequence, fewer values might be returned. The sort sequence
|
|
causes values that contain the same characters to be weighted the same. If
|
|
'MGR', 'Mgr', and 'mgr' were all in the same table, only one of these values
|
|
is returned. </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>
|
|
</body>
|
|
</html> |