2963:SQL: Summarizing a Calculated Column
KEYWORDS: SQL DATABASE SUM CALCULATED AREA: Database Programming
Occasionally in a Delphi application that uses SQL to access data, it
becomes necessary to summarize calculated data. That is, to create a
calculated column and apply the SUM function to it.
When performing this operation against SQL tables (such as those for the
Local InterBase Server), it is a simple matter of enclosing the
calculation within the SUM function. For example, using the sample table
EMPLOYEE (in the EMPLOYEE.GDB database):
SELECT SUM(SALARY / 12)
FROM EMPLOYEE
This same methodology can also be used when the returned data set is to be
grouped by the value in another column with a GROUP BY clause:
SELECT EMP_NO, SUM(SALARY / 12)
FROM EMPLOYEE
GROUP BY EMP_NO
ORDER BY EMP_NO
While SQL databases support the summarization of calculated columns,
local SQL will not. Other means would be needed to obtain the results,
such as copying the results of a query with a calculated column to a
temporary table (as with a TBatchMove component) and then using a TQuery
component to summarize the data in the temporary table.
TI