DATABASE & SQL/PLSQL

adplus-dvertising
adplus-dvertising
Using COMPUTE BY Clause
Previous Home Next

Used to generate summary rows using aggregate function in the query results. COMPUTE BY clause can be used to calculate summary values of the result set on a group of data.The main difference between GROUP BY and COMPUTE BY Clause is,

GROUP BY Clause is used to generate a group summary report and does not produce individual table rows in the result set whereas COMPUTE BY Clause generate the summary report with individual data rows from the table.

SELECT  column_name
FROM  table_name
ORDER BY  column_name
COMPUTE  aggregate_function (column_name)  BY  column_name 1...column_name n

Example:

SELECT  type, advance
FROM  titles
ORDER BY  type
COMPUTE AVG (advance)  BY  type

output

Some points regarding the use of the COMPUTE and COMPUTE BY

  1. The DISTINCT keyword cannot be used with the aggregate function
  2. All columns referred to in the COMPUTE clause must appear in the select column list
  3. The ORDER BY Clause must be used whenever the COMPUTE BY Clause is used
  4. The ORDER BY Clause can be eliminate only when the COMPUTE BY Clause is used
  5. The column listed in the COMPUTE BY Clause must match the columns used in the ORDER BY Clause
  6. More than one COMPUTE clause can be used in the SELECT statement to produce a result with subtotals and grand total
  7. The different aggregate function can be used on more than one column with the COMPUTE BY Clause
  8. More than one column or expression can be specified after the COMPUTE BY clause. The order of columns or expression used in the COMPUTE BY clause must match the order of columns or expression specified in the ORDER BY Clause.
Previous Home Next