Totals and Subtotals

SQL*Plus allows you to print totals and subtotals in a report. To do this, you use a combination of the BREAK command and one or more COMPUTE commands. This section continues where the previous section left off. It will show you how to modify the master/detail report created earlier so that it totals the hours and dollars by project and by employee. You will also see how to print grand totals for these columns at the end of the report.

The COMPUTE Command

The COMPUTE command defines summary calculations needed in a report. You can use COMPUTE in conjunction with BREAK to calculate and print column totals, averages, minimum and maximum values, and so on. These calculations are performed by SQL*Plus as the report runs.

Syntax of the COMPUTE command

The syntax for the COMPUTE command looks like this:

COMP[UTE] [{AVG|COU[NT]|MAX[IMUM]|MIN[IMUM]|
          NUM[BER]|STD|SUM|VAR[IANCE]}... [LABEL label_text]
          OF column_name... 
          ON {group_column_name|ROW|REPORT}...]

where:

COMP[UTE]

May be abbreviated to COMP. Entering COMPUTE with no parameters causes SQL*Plus to list all currently defined computations.

AVG

Computes the average of all non-null values for a column. AVG only applies to columns of type NUMBER.

COU[NT]

Computes the total number of non-null values for a column. COUNT may be used with columns of any datatype, and may be abbreviated to COU.

MAX[IMUM]

Computes the maximum value returned for a column. MAXIMUM may be abbreviated as MAX, and applies to columns of type NUMBER, CHAR, ...

Get Oracle SQL*Plus: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.