Published by Patrick Mutisya · 14 days ago
Be able to use formulae and functions to perform calculations at run time, including:
Calculations at run time allow users to:
A formula field stores a calculation that is performed each time a record is displayed or updated. The result is not stored permanently; it is generated on the fly.
Typical syntax (varies by DBMS):
FieldName: Expression
Example using arithmetic operators:
\$\text{TotalPrice} = \text{Quantity} \times \text{UnitPrice}\$
Aggregate functions operate on a set of records and return a single value.
| Function | Purpose | Syntax example | Result (example data) |
|---|---|---|---|
| SUM() | Adds all values in a numeric field. | SUM(Quantity) | 150 (if quantities are 30, 45, 75) |
| A \cdot G() | Calculates the average of a numeric field. | A \cdot G(UnitPrice) | 12.5 |
| MAX() | Finds the highest value in a field. | MAX(TotalPrice) | 375 |
| MIN() | Finds the lowest value in a field. | MIN(TotalPrice) | 90 |
| COUNT() | Counts the number of records (or non‑null entries). | COUNT(*) | 5 (five records) |
Consider a simple sales table that records each transaction.
| TransactionID | Item | Quantity | UnitPrice ($) | Total ($) |
|---|---|---|---|---|
| 1 | Notebook | 10 | 2.50 | \$10 \times 2.50 = 25.00\$ |
| 2 | Pen | 25 | 0.80 | \$25 \times 0.80 = 20.00\$ |
| 3 | Folder | 5 | 3.20 | \$5 \times 3.20 = 16.00\$ |
| 4 | Marker | 12 | 1.50 | \$12 \times 1.50 = 18.00\$ |
| 5 | Stapler | 3 | 7.00 | \$3 \times 7.00 = 21.00\$ |
SUM(Total)\$\text{Total Sales} = \sum{i=1}^{5} \text{Total}i = 25 + 20 + 16 + 18 + 21 = 100\$
A \cdot G(UnitPrice)\$\text{Average Unit Price} = \frac{2.50 + 0.80 + 3.20 + 1.50 + 7.00}{5} = 3.00\$
MAX(Total)\$\text{Maximum Total} = 25\$
MIN(Total)\$\text{Minimum Total} = 16\$
COUNT(TransactionID)\$\text{Count} = 5\$
It is possible to embed arithmetic inside an aggregate function. Example:
SUM(Quantity * UnitPrice) – adds the product of quantity and unit price for each record, giving the same result as SUM(Total).
\$\sum{i=1}^{5} (\text{Quantity}i \times \text{UnitPrice}_i) = 100\$
/.| Operation | Formula / Function | Example |
|---|---|---|
| Addition | FieldA + FieldB | \$5 + 3 = 8\$ |
| Subtraction | FieldA - FieldB | \$10 - 4 = 6\$ |
| Multiplication | FieldA * FieldB | \$7 \times 2 = 14\$ |
| Division | FieldA / FieldB | \$20 \div 5 = 4\$ |
| Sum | SUM(Field) | \$\sum 1..n = 100\$ |
| Average | A \cdot G(Field) | \$\frac{\sum}{n} = 12.5\$ |
| Maximum | MAX(Field) | \$\max = 75\$ |
| Minimum | MIN(Field) | \$\min = 3\$ |
| Count | COUNT(Field) | \$\text{records} = 5\$ |