Creating Summarized Reports Using COMPUTE

Transact-SQL also has a mechanism for creating summarized database reports. The command, COMPUTE, has very similar syntax to its counterpart in SQL*Plus. (See Bonus Day 3, “Using Oracle SQL*Plus to Satisfy Reporting Needs.”)

The following query produces a report showing all batters, the number of home runs hit by each batter, and the total number of home runs hit by all batters:

select name, homeruns
from batters
compute sum(homeruns)

In the previous example, COMPUTE alone performed computations on the report as a whole. COMPUTE BY, on the other hand, performs computations on specified groups and the ...

Get Sams Teach Yourself SQL in 21 Days, Fourth Edition 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.