SQL/MP Query Guide

Retrieving Data: How to Write Queries
HP NonStop SQL/MP Query Guide524488-003
1-67
Computing Row Value as a Percent of All Row
Values
1. Create a temporary table to contain the average salary for each department:
>> CREATE TABLE TEMPTABS.AVGTEMP (
+> DEPTNUM NUMERIC (4) UNSIGNED NOT NULL,
+> AVGSAL NUMERIC (6) UNSIGNED NOT NULL)
+> CATALOG TEMPTABS ;
2. Insert the department number and average salary into the temporary table:
>> INSERT INTO TEMPTABS.AVGTEMP
+> (SELECT DEPTNUM, AVG(SALARY) FROM PERSNL.EMPLOYEE
+> GROUP BY DEPTNUM) ;
3. Select the information for the report. Include an expression in the select list to
compute the percent of the department average:
>> SELECT E.DEPTNUM, EMPNUM, LAST_NAME, SALARY,
+> SALARY/AVGSAL*100.00
+> FROM PERSNL.EMPLOYEE E, TEMPTABS.AVGTEMP A
+> WHERE E.DEPTNUM = A.DEPTNUM;
S> DETAIL DEPTNUM, EMPNUM, LAST_NAME,
+> SALARY AS F10.2,
+> COL 5 AS F10.2 HEADING "PCT OF AVG" ;
S> LIST ALL ;
DEPTNUM EMPNUM LAST_NAME SALARY PCT OF AVG
------- ------ ---------------- ---------- ----------
1000 23 HOWARD 137000.10 263.46
1000 202 CLARK 25000.75 48.08
1000 208 CRAMER 19000.00 36.54
. . . . .
. . . . .
. . . . .
9000 1 GREEN 175500.00 165.00
9000 337 CLARK 37000.00 34.82
--- 57 row(s) selected.
You can drop the temporary table or purge the data and keep the table for use in
producing future reports.