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

191 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="Use table expressions" />
<meta name="abstract" content="You can use table expressions to specify an intermediate result table." />
<meta name="description" content="You can use table expressions to specify an intermediate result table." />
<meta name="DC.subject" content="SELECT statement, use table expressions, table, derived, derived table, examples, use table expressions" />
<meta name="keywords" content="SELECT statement, use table expressions, table, derived, derived table, examples, use table expressions" />
<meta name="DC.Relation" scheme="URI" content="rbafytexas.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="rbafytabexp" />
<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>Use table expressions</title>
</head>
<body id="rbafytabexp"><a name="rbafytabexp"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Use table expressions</h1>
<div><p>You can use table expressions to specify an intermediate result
table.</p>
<div class="section"><p>Table expressions can be used in place of a view to avoid creating
the view when general use of the view is not required. Table expressions consist
of nested table expressions (also called derived tables) and common table
expressions.</p>
</div>
<div class="section"><p>Nested table expressions are specified within parentheses in the
FROM clause. For example, suppose you want a result table that shows the manager
number, department number, and maximum salary for each department. The manager
number is in the DEPARTMENT table, the department number is in both the DEPARTMENT
and EMPLOYEE tables, and the salaries are in the EMPLOYEE table. You can
use a table expression in the FROM clause to select the maximum salary for
each department. You can also add a correlation name, T2, following the nested
table expression to name the derived table. The outer select then uses T2
to qualify columns that are selected from the derived table, in this case
MAXSAL and WORKDEPT. Note that the MAX(SALARY) column selected in the nested
table expression must be named in order to be referenced in the outer select.
The AS clause is used to do that. </p>
<pre> <strong>SELECT</strong> MGRNO, T1.DEPTNO, MAXSAL
<strong>FROM</strong> CORPDATA.DEPARTMENT T1,
(<strong>SELECT</strong> <strong>MAX</strong>(SALARY) <strong>AS</strong> MAXSAL, WORKDEPT
<strong>FROM</strong> CORPDATA.EMPLOYEE E1
<strong>GROUP BY</strong> WORKDEPT) T2
<strong>WHERE</strong> T1.DEPTNO = T2.WORKDEPT
<strong>ORDER BY</strong> DEPTNO</pre>
</div>
<div class="section"><p>The result of the query is: </p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" width="100%" frame="hsides" border="1" rules="all"><thead align="left"><tr><th align="left" valign="bottom" width="33.33333333333333%" id="d0e76">MGRNO</th>
<th align="left" valign="bottom" width="33.33333333333333%" id="d0e78">DEPTNO</th>
<th align="left" valign="bottom" width="33.33333333333333%" id="d0e80">MAXSAL</th>
</tr>
</thead>
<tbody><tr><td align="left" valign="top" width="33.33333333333333%" headers="d0e76 ">000010</td>
<td align="left" valign="top" width="33.33333333333333%" headers="d0e78 ">A00</td>
<td align="left" valign="top" width="33.33333333333333%" headers="d0e80 ">52750.00</td>
</tr>
<tr><td align="left" valign="top" width="33.33333333333333%" headers="d0e76 ">000020</td>
<td align="left" valign="top" width="33.33333333333333%" headers="d0e78 ">B01</td>
<td align="left" valign="top" width="33.33333333333333%" headers="d0e80 ">41250.00</td>
</tr>
<tr><td align="left" valign="top" width="33.33333333333333%" headers="d0e76 ">000030</td>
<td align="left" valign="top" width="33.33333333333333%" headers="d0e78 ">C01</td>
<td align="left" valign="top" width="33.33333333333333%" headers="d0e80 ">38250.00</td>
</tr>
<tr><td align="left" valign="top" width="33.33333333333333%" headers="d0e76 ">000060</td>
<td align="left" valign="top" width="33.33333333333333%" headers="d0e78 ">D11</td>
<td align="left" valign="top" width="33.33333333333333%" headers="d0e80 ">32250.00</td>
</tr>
<tr><td align="left" valign="top" width="33.33333333333333%" headers="d0e76 ">000070</td>
<td align="left" valign="top" width="33.33333333333333%" headers="d0e78 ">D21</td>
<td align="left" valign="top" width="33.33333333333333%" headers="d0e80 ">36170.00</td>
</tr>
<tr><td align="left" valign="top" width="33.33333333333333%" headers="d0e76 ">000050</td>
<td align="left" valign="top" width="33.33333333333333%" headers="d0e78 ">E01</td>
<td align="left" valign="top" width="33.33333333333333%" headers="d0e80 ">40175.00</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e76 ">000090</td>
<td valign="top" width="33.33333333333333%" headers="d0e78 ">E11</td>
<td valign="top" width="33.33333333333333%" headers="d0e80 ">29750.00</td>
</tr>
<tr><td valign="top" width="33.33333333333333%" headers="d0e76 ">000100</td>
<td valign="top" width="33.33333333333333%" headers="d0e78 ">E21</td>
<td valign="top" width="33.33333333333333%" headers="d0e80 ">26150.00</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section"><p>Common table expressions can be specified before the full-select
in a SELECT statement, an INSERT statement, or a CREATE VIEW statement. They
can be used when the same result table needs to be shared in a full-select.
Common table expressions are preceded with the keyword WITH.</p>
</div>
<div class="section"><p>For example, suppose you want a table that shows the minimum and
maximum of the average salary of a certain set of departments. The first character
of the department number has some meaning and you want to get the minimum
and maximum for those departments that start with the letter 'D' and those
that start with the letter 'E'. You can use a common table expression to
select the average salary for each department. Again, you must name the derived
table; in this case, the name is DT. You can then specify a SELECT statement
using a WHERE clause to restrict the selection to only the departments that
begin with a certain letter. Specify the minimum and maximum of column AVGSAL
from the derived table DT. Specify a UNION to get the results for the letter
'E' and the results for the letter 'D'. </p>
<pre><strong>WITH</strong> DT <strong>AS</strong> (<strong>SELECT</strong> E.WORKDEPT <strong>AS</strong> DEPTNO, <strong>AVG</strong>(SALARY) <strong>AS</strong> AVGSAL
<strong>FROM</strong> CORPDATA.DEPARTMENT D , CORPDATA.EMPLOYEE E
<strong>WHERE</strong> D.DEPTNO = E.WORKDEPT
<strong>GROUP BY</strong> E.WORKDEPT)
<strong>SELECT</strong> 'E', <strong>MAX</strong>(AVGSAL), <strong>MIN</strong>(AVGSAL) <strong>FROM</strong> DT
<strong>WHERE</strong> DEPTNO <strong>LIKE</strong> 'E%'
<strong>UNION</strong>
<strong>SELECT</strong> 'D', <strong>MAX</strong>(AVGSAL), <strong>MIN</strong>(AVGSAL) <strong>FROM</strong> DT
<strong>WHERE</strong> DEPTNO <strong>LIKE</strong> 'D%'</pre>
</div>
<div class="section"><p>The result of the query is: </p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" frame="hsides" border="1" rules="all"><thead align="left"><tr><th valign="top" width="29.96632996632997%" id="d0e222">&nbsp;</th>
<th valign="top" width="35.01683501683502%" id="d0e223">MAX(AVGSAL)</th>
<th valign="top" width="35.01683501683502%" id="d0e225">MIN(AVGSAL)</th>
</tr>
</thead>
<tbody><tr><td valign="top" width="29.96632996632997%" headers="d0e222 ">E</td>
<td valign="top" width="35.01683501683502%" headers="d0e223 ">40175.00</td>
<td valign="top" width="35.01683501683502%" headers="d0e225 ">21020.00</td>
</tr>
<tr><td valign="top" width="29.96632996632997%" headers="d0e222 ">D</td>
<td valign="top" width="35.01683501683502%" headers="d0e223 ">25668.57</td>
<td valign="top" width="35.01683501683502%" headers="d0e225 ">25147.27</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section"><p>Suppose you want to write a query against your ordering database
that will return the top 5 items (in total quantity ordered) within the last
1000 orders from customers who also ordered item 'XXX'. </p>
<pre><strong>WITH</strong> X <strong>AS</strong> (<strong>SELECT</strong> ORDER_ID, CUST_ID
<strong>FROM</strong> ORDERS
<strong>ORDER BY</strong> ORD_DATE <strong>DESC</strong>
<strong>FETCH FIRST</strong> 1000 <strong>ROWS ONLY</strong>),
Y <strong>AS</strong> (<strong>SELECT</strong> CUST_ID, LINE_ID, ORDER_QTY
<strong>FROM</strong> X, ORDERLINE
<strong>WHERE</strong> X.ORDER_ID = ORDERLINE.ORDER_ID)
<strong>SELECT</strong> LINE_ID
<strong>FROM</strong> (<strong>SELECT</strong> LINE_ID
<strong>FROM</strong> Y
<strong>WHERE</strong> Y.CUST_ID <strong>IN</strong> (<strong>SELECT DISTINCT</strong> CUST_ID
<strong>FROM</strong> Y
<strong>WHERE</strong> LINE.ID = 'XXX' )
<strong>GROUP BY</strong> LINE_ID
<strong>ORDER BY SUM</strong>(ORDER_QTY) <strong>DESC</strong>)
<strong>FETCH FIRST</strong> 5 <strong>ROWS ONLY</strong></pre>
<p>The first common table
expression (X) returns the most recent 1000 order numbers. The result is
ordered by the date in descending order and then only the first 1000 of those
ordered rows are returned as the result table.</p>
</div>
<div class="section"><p>The second common table expression (Y) joins the most recent 1000
orders with the line item table and returns (for each of the 1000 orders)
the customer, line item, and quantity of the line item for that order.</p>
</div>
<div class="section"><p>The derived table in the main select statement returns the line
items for the customers who are in the top 1000 orders who ordered item XXX.
The results for all customers who ordered XXX are then grouped by the line
item and the groups are ordered by the total quantity of the line item.</p>
</div>
<div class="section"><p>Finally, the outer select selects only the first 5 rows from the
ordered list that the derived table 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>
</body>
</html>