ibm-information-center/dist/eclipse/plugins/i5OS.ic.dbp_5.4.0.1/rbafogrpex1.htm

282 lines
16 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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="Example: Summarize data from database file records (grouping)" />
<meta name="abstract" content="This example shows how to use the group processing functions to summarize data from existing database records." />
<meta name="description" content="This example shows how to use the group processing functions to summarize data from existing database records." />
<meta name="DC.Relation" scheme="URI" content="rbafogrp.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="rbafogrpex1" />
<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>Example: Summarize data from database file records (grouping)</title>
</head>
<body id="rbafogrpex1"><a name="rbafogrpex1"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Summarize data from database file records (grouping)</h1>
<div><p>This example shows how to use the group processing functions to
summarize data from existing database records.</p>
<div class="section"><p>Assume that you want to group the data by customer number and
analyze the amount field. Your database file is FILEA and you create a file
named FILEAA containing a record format with the following fields:</p>
</div>
<table cellpadding="4" cellspacing="0" border="1" class="tableborder"><tr><td>
<table cellpadding="4" cellspacing="0" summary="" width="100%" border="0"><thead align="left"><tr><th align="left" valign="bottom" width="25%" id="d0e21">FILEA</th>
<th align="left" valign="bottom" width="75%" id="d0e23">FILEAA</th>
</tr>
</thead>
<tbody><tr><td align="left" valign="top" width="25%" headers="d0e21 ">Cust</td>
<td align="left" valign="top" width="75%" headers="d0e23 ">Cust</td>
</tr>
<tr><td align="left" valign="top" width="25%" headers="d0e21 ">Type</td>
<td align="left" valign="top" width="75%" headers="d0e23 ">Count (count of records per customer)</td>
</tr>
<tr><td align="left" valign="top" width="25%" headers="d0e21 ">Amt</td>
<td align="left" valign="top" width="75%" headers="d0e23 ">Amtsum (summation of the amount field)</td>
</tr>
<tr><td align="left" valign="top" width="25%" headers="d0e21 "> </td>
<td align="left" valign="top" width="75%" headers="d0e23 ">Amtavg (average of the amount field)</td>
</tr>
<tr><td align="left" valign="top" width="25%" headers="d0e21 "> </td>
<td align="left" valign="top" width="75%" headers="d0e23 ">Amtmax (maximum value of the amount field)</td>
</tr>
</tbody>
</table>
</td></tr></table>
<div class="section"><p>When you define the fields in the new file, you must ensure that
they are large enough to hold the results. For example, if the <em>Amt</em> field
is defined as 5 digits, you might need to define the <em>Amtsum</em> field as
7 digits. Any arithmetic overflow causes your program to end abnormally.</p>
</div>
<div class="section"><p>Assume that the records in FILEA have the following values:</p>
</div>
<table cellpadding="4" cellspacing="0" border="1" class="tableborder"><tr><td>
<table cellpadding="4" cellspacing="0" summary="" width="100%" border="0"><thead align="left"><tr><th align="center" valign="bottom" width="33.33333333333333%" id="d0e70">Cust</th>
<th align="center" valign="bottom" width="33.33333333333333%" id="d0e72">Type</th>
<th align="center" valign="bottom" width="33.33333333333333%" id="d0e74">Amt</th>
</tr>
</thead>
<tbody><tr><td align="center" valign="top" width="33.33333333333333%" headers="d0e70 ">001</td>
<td align="center" valign="top" width="33.33333333333333%" headers="d0e72 ">A</td>
<td align="center" valign="top" width="33.33333333333333%" headers="d0e74 ">500.00</td>
</tr>
<tr><td align="center" valign="top" width="33.33333333333333%" headers="d0e70 ">001</td>
<td align="center" valign="top" width="33.33333333333333%" headers="d0e72 ">B</td>
<td align="center" valign="top" width="33.33333333333333%" headers="d0e74 ">700.00</td>
</tr>
<tr><td align="center" valign="top" width="33.33333333333333%" headers="d0e70 ">004</td>
<td align="center" valign="top" width="33.33333333333333%" headers="d0e72 ">A</td>
<td align="center" valign="top" width="33.33333333333333%" headers="d0e74 ">100.00</td>
</tr>
<tr><td align="center" valign="top" width="33.33333333333333%" headers="d0e70 ">002</td>
<td align="center" valign="top" width="33.33333333333333%" headers="d0e72 ">A</td>
<td align="center" valign="top" width="33.33333333333333%" headers="d0e74 ">1200.00</td>
</tr>
<tr><td align="center" valign="top" width="33.33333333333333%" headers="d0e70 ">003</td>
<td align="center" valign="top" width="33.33333333333333%" headers="d0e72 ">B</td>
<td align="center" valign="top" width="33.33333333333333%" headers="d0e74 ">900.00</td>
</tr>
<tr><td align="center" valign="top" width="33.33333333333333%" headers="d0e70 ">001</td>
<td align="center" valign="top" width="33.33333333333333%" headers="d0e72 ">A</td>
<td align="center" valign="top" width="33.33333333333333%" headers="d0e74 ">300.00</td>
</tr>
<tr><td align="center" valign="top" width="33.33333333333333%" headers="d0e70 ">004</td>
<td align="center" valign="top" width="33.33333333333333%" headers="d0e72 ">A</td>
<td align="center" valign="top" width="33.33333333333333%" headers="d0e74 ">300.00</td>
</tr>
<tr><td align="center" valign="top" width="33.33333333333333%" headers="d0e70 ">003</td>
<td align="center" valign="top" width="33.33333333333333%" headers="d0e72 ">B</td>
<td align="center" valign="top" width="33.33333333333333%" headers="d0e74 ">600.00</td>
</tr>
</tbody>
</table>
</td></tr></table>
<div class="section"><div class="p">You then create a program (PGMG) using FILEAA as input to print
the records.<pre>OVRDBF FILE(FILEAA) TOFILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) FORMAT(FILEAA) KEYFLD(CUST) +
GRPFLD(CUST) MAPFLD((COUNT '%COUNT') +
(AMTSUM '%SUM(AMT)') +
(AMTAVG '%AVG(AMT)') +
(AMTMAX '%MAX(AMT)'))
CALL PGM(PGMG) /* Created using file FILEAA as input */
CLOF OPNID(FILEA)
DLTOVR FILE(FILEAA)</pre>
</div>
</div>
<div class="section"><p>The records retrieved by the program appear as:</p>
</div>
<table cellpadding="4" cellspacing="0" border="1" class="tableborder"><tr><td>
<table cellpadding="4" cellspacing="0" summary="" width="100%" border="0"><thead align="left"><tr><th align="center" valign="bottom" width="20%" id="d0e150">Cust</th>
<th align="center" valign="bottom" width="20%" id="d0e152">Count</th>
<th align="center" valign="bottom" width="20%" id="d0e154">Amtsum</th>
<th align="center" valign="bottom" width="20%" id="d0e156">Amtavg</th>
<th align="center" valign="bottom" width="20%" id="d0e158">Amtmax</th>
</tr>
</thead>
<tbody><tr><td align="center" valign="top" width="20%" headers="d0e150 ">001</td>
<td align="center" valign="top" width="20%" headers="d0e152 ">3</td>
<td align="center" valign="top" width="20%" headers="d0e154 ">1500.00</td>
<td align="center" valign="top" width="20%" headers="d0e156 ">500.00</td>
<td align="center" valign="top" width="20%" headers="d0e158 ">700.00</td>
</tr>
<tr><td align="center" valign="top" width="20%" headers="d0e150 ">002</td>
<td align="center" valign="top" width="20%" headers="d0e152 ">1</td>
<td align="center" valign="top" width="20%" headers="d0e154 ">1200.00</td>
<td align="center" valign="top" width="20%" headers="d0e156 ">1200.00</td>
<td align="center" valign="top" width="20%" headers="d0e158 ">1200.00</td>
</tr>
<tr><td align="center" valign="top" width="20%" headers="d0e150 ">003</td>
<td align="center" valign="top" width="20%" headers="d0e152 ">2</td>
<td align="center" valign="top" width="20%" headers="d0e154 ">1500.00</td>
<td align="center" valign="top" width="20%" headers="d0e156 ">750.00</td>
<td align="center" valign="top" width="20%" headers="d0e158 ">900.00</td>
</tr>
<tr><td align="center" valign="top" width="20%" headers="d0e150 ">004</td>
<td align="center" valign="top" width="20%" headers="d0e152 ">2</td>
<td align="center" valign="top" width="20%" headers="d0e154 ">400.00</td>
<td align="center" valign="top" width="20%" headers="d0e156 ">200.00</td>
<td align="center" valign="top" width="20%" headers="d0e158 ">300.00</td>
</tr>
</tbody>
</table>
</td></tr></table>
<div class="section"> <div class="note"><span class="notetitle">Note:</span> If you specify the GRPFLD parameter, the groups might not
appear in ascending sequence. To ensure a specific sequence, you should specify
the KEYFLD parameter.</div>
</div>
<div class="section"><div class="p">Assume that you want to print only the summary records in this
example in which the <em>Amtsum</em> value is greater than 700.00. Because the <em>Amtsum</em> field
is an aggregate field for a given customer, use the GRPSLT parameter to specify
selection after grouping. Add the GRPSLT parameter: <pre>GRPSLT('AMTSUM *GT 700.00')</pre>
</div>
</div>
<div class="section"><p>The records retrieved by your program are:</p>
</div>
<table cellpadding="4" cellspacing="0" border="1" class="tableborder"><tr><td>
<table cellpadding="4" cellspacing="0" summary="" width="100%" border="0"><thead align="left"><tr><th align="center" valign="bottom" width="20%" id="d0e232">Cust</th>
<th align="center" valign="bottom" width="20%" id="d0e234">Count</th>
<th align="center" valign="bottom" width="20%" id="d0e236">Amtsum</th>
<th align="center" valign="bottom" width="20%" id="d0e238">Amtavg</th>
<th align="center" valign="bottom" width="20%" id="d0e240">Amtmax</th>
</tr>
</thead>
<tbody><tr><td align="center" valign="top" width="20%" headers="d0e232 ">001</td>
<td align="center" valign="top" width="20%" headers="d0e234 ">3</td>
<td align="center" valign="top" width="20%" headers="d0e236 ">1500.00</td>
<td align="center" valign="top" width="20%" headers="d0e238 ">500.00</td>
<td align="center" valign="top" width="20%" headers="d0e240 ">700.00</td>
</tr>
<tr><td align="center" valign="top" width="20%" headers="d0e232 ">002</td>
<td align="center" valign="top" width="20%" headers="d0e234 ">1</td>
<td align="center" valign="top" width="20%" headers="d0e236 ">1200.00</td>
<td align="center" valign="top" width="20%" headers="d0e238 ">1200.00</td>
<td align="center" valign="top" width="20%" headers="d0e240 ">1200.00</td>
</tr>
<tr><td align="center" valign="top" width="20%" headers="d0e232 ">003</td>
<td align="center" valign="top" width="20%" headers="d0e234 ">2</td>
<td align="center" valign="top" width="20%" headers="d0e236 ">1500.00</td>
<td align="center" valign="top" width="20%" headers="d0e238 ">750.00</td>
<td align="center" valign="top" width="20%" headers="d0e240 ">900.00</td>
</tr>
</tbody>
</table>
</td></tr></table>
<div class="section"><p>The Open Query File (OPNQRYF) command supports selection both
before grouping (QRYSLT parameter) and after grouping (GRPSLT parameter).</p>
</div>
<div class="section"><div class="p">Assume that you want to select additional customer records in
which the <em>Type</em> field is equal to A. Because <em>Type</em> is a field
in the record format for file FILEA and not an aggregate field, you add the
QRYSLT statement to select before grouping as follows: <pre>QRYSLT('TYPE *EQ "A" ')</pre>
</div>
</div>
<div class="section"><div class="note"><span class="notetitle">Note:</span> Fields used for selection do not have to appear in the format
processed by the program.</div>
</div>
<div class="section"><p>The records retrieved by your program are:</p>
</div>
<table cellpadding="4" cellspacing="0" border="1" class="tableborder"><tr><td>
<table cellpadding="4" cellspacing="0" summary="" width="100%" border="0"><thead align="left"><tr><th align="center" valign="bottom" width="20%" id="d0e305">Cust</th>
<th align="center" valign="bottom" width="20%" id="d0e307">Count</th>
<th align="center" valign="bottom" width="20%" id="d0e309">Amtsum</th>
<th align="center" valign="bottom" width="20%" id="d0e311">Amtavg</th>
<th align="center" valign="bottom" width="20%" id="d0e313">Amtmax</th>
</tr>
</thead>
<tbody><tr><td align="center" valign="top" width="20%" headers="d0e305 ">001</td>
<td align="center" valign="top" width="20%" headers="d0e307 ">2</td>
<td align="center" valign="top" width="20%" headers="d0e309 ">800.00</td>
<td align="center" valign="top" width="20%" headers="d0e311 ">400.00</td>
<td align="center" valign="top" width="20%" headers="d0e313 ">500.00</td>
</tr>
<tr><td align="center" valign="top" width="20%" headers="d0e305 ">002</td>
<td align="center" valign="top" width="20%" headers="d0e307 ">1</td>
<td align="center" valign="top" width="20%" headers="d0e309 ">1200.00</td>
<td align="center" valign="top" width="20%" headers="d0e311 ">1200.00</td>
<td align="center" valign="top" width="20%" headers="d0e313 ">1200.00</td>
</tr>
</tbody>
</table>
</td></tr></table>
<div class="section"><div class="note"><span class="notetitle">Note:</span> The values for CUST 001 changed because the selection took
place before the grouping took place.</div>
</div>
<div class="section"><div class="p">Assume that you want to arrange the output
by the <em>Amtavg</em> field in descending sequence, in addition to the previous
QRYSLT parameter value. You can do this by changing the KEYFLD parameter on
the OPNQRYF command as: <pre>KEYFLD((AMTAVG *DESCEND))</pre>
</div>
</div>
<div class="section"><p>The records retrieved by your program are:</p>
</div>
<table cellpadding="4" cellspacing="0" border="1" class="tableborder"><tr><td>
<table cellpadding="4" cellspacing="0" summary="" width="100%" border="0"><thead align="left"><tr><th align="center" valign="bottom" width="20%" id="d0e369">Cust</th>
<th align="center" valign="bottom" width="20%" id="d0e371">Count</th>
<th align="center" valign="bottom" width="20%" id="d0e373">Amtsum</th>
<th align="center" valign="bottom" width="20%" id="d0e375">Amtavg</th>
<th align="center" valign="bottom" width="20%" id="d0e377">Amtmax</th>
</tr>
</thead>
<tbody><tr><td align="center" valign="top" width="20%" headers="d0e369 ">002</td>
<td align="center" valign="top" width="20%" headers="d0e371 ">1</td>
<td align="center" valign="top" width="20%" headers="d0e373 ">1200.00</td>
<td align="center" valign="top" width="20%" headers="d0e375 ">1200.00</td>
<td align="center" valign="top" width="20%" headers="d0e377 ">1200.00</td>
</tr>
<tr><td align="center" valign="top" width="20%" headers="d0e369 ">001</td>
<td align="center" valign="top" width="20%" headers="d0e371 ">2</td>
<td align="center" valign="top" width="20%" headers="d0e373 ">800.00</td>
<td align="center" valign="top" width="20%" headers="d0e375 ">400.00</td>
<td align="center" valign="top" width="20%" headers="d0e377 ">500.00</td>
</tr>
</tbody>
</table>
</td></tr></table>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafogrp.htm" title="The group processing function allows you to summarize data from existing database records.">Summarize data from database file records (grouping)</a></div>
</div>
</div>
</body>
</html>