Report features: grouping, filtering and calculations
How report features — grouping, filtering and calculations — let engineers convert raw tables into useful aggregated and filtered insights for dashboards and BI reports.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Business reports show summaries and patterns from raw data so managers can decide. Engineers implement grouping, filtering and calculations in report queries and report-builder tools (SQL, report designers, Power BI/Tableau) to produce aggregated numbers, filtered views, computed KPIs and formatted outputs for dashboards and MIS.
Key ideas
Begin with definitions (each bolded the first time it appears):
- Record (row): one tuple of values in a table; e.g., one sale (date, product, amount).
- Field (column): one attribute in a record, like Amount or Region.
- Filtering: selecting a subset of records that meet a condition. Example: keep records where Amount > 1000.
- Grouping: combining records that have the same value(s) in one or more fields, then treating the group as a unit for aggregation. Example: group by Region.
- Aggregate function: a function that maps a set of values to a single value (examples: SUM, COUNT, AVG, MIN, MAX). Aggregates are applied per group.
- Calculated field (computed column / expression): a new value created from existing fields using arithmetic or conditional logic, e.g., Profit = SalePrice - Cost.
- Projection: selecting which fields appear in output (SELECT in SQL). In grouped queries, only grouped columns and aggregate expressions can appear in the SELECT list.
- HAVING vs WHERE: WHERE filters rows before grouping; HAVING filters groups after aggregation.
Exact statements and common university-style facts to remember:
SQL query processing order (simplified, exact order often asked):
- FROM (joins and cross-product) -> WHERE (row filters) -> GROUP BY (make groups) -> HAVING (filter groups) -> SELECT (compute projections, aggregates) -> ORDER BY -> LIMIT/OFFSET.
- Assumption: no subqueries or window functions; complexity added by those is usually exam extension.
Aggregation rule (classic constraint): In a grouped query, every selected non-aggregate column must appear in the GROUP BY set. Statement:
- If SELECT lists columns C1..Cn and also aggregate functions, then for correctness every column Ci that is not inside an aggregate expression must be present in GROUP BY.
- Exception: SQL extensions (MySQL-only non-standard behaviour with ANY_VALUE) or functional dependency (SQL standard: column functionally dependent on GROUP BY columns may be allowed). Exams expect the basic rule unless told otherwise.
NULL handling for aggregates:
- COUNT(column) counts non-null values only; COUNT(*) counts rows including rows with nulls in other columns.
- SUM, AVG ignore NULL values in the set; if all values are NULL the result is NULL.
- MIN, MAX ignore NULLs. Also, in GROUP BY, all rows with NULL in the same grouping column are placed into a single group (per SQL standard and major DBMSs).
Grouping sets and roll-up (common exam topics):
- GROUPING SETS, ROLLUP and CUBE are ways to request multiple grouped subtotals in one query. Example description to draw: table of sales by Region and Product, then show rows for (Region, Product), subtotal per Region (Product = NULL meaning subtotal), and grand total (both NULL).
- Assumption: exam may ask to describe result rows and meaning of NULL placeholders.
Window functions vs aggregation:
- Window function computes a value for each row using a frame (e.g., running total) without collapsing rows. Aggregation collapses rows per group. Example: SUM(Amount) OVER (PARTITION BY Region ORDER BY Date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) gives running total per Region.
Diagram instructions often required in exams:
- To show grouping: draw input table (columns Date, Region, Product, Amount) with 6–8 rows. Draw arrows from rows that share Region = "South" to a box labeled "Group: Region=South". Next to it write aggregated result lines: Region, SUM(Amount), COUNT(*), AVG(Amount).
- To show WHERE vs HAVING: draw two-step flowchart: Dataset -> apply WHERE (filter rows) -> GROUP BY (groups) -> apply HAVING (filter groups) -> output.
Formulas
List of formulas / syntaxes to remember (each on own line):
AVG = SUM(values) ÷ COUNT(values) — where AVG is the arithmetic mean of a numeric set. Symbols: SUM(values) is sum of numeric values (unit: same as values, e.g., ₹), COUNT(values) is number of non-null values (unit: count, dimensionless). Applies when computing average in reports.
% of total = (PART / TOTAL) × 100 — PART is group sum, TOTAL is grand-sum. PART and TOTAL units same (e.g., ₹); result in percent (%). Use when computing share of group.
RunningTotal_i = SUM_{j=1..i} value_j — running total up to row i ordered by a chosen column (like date). Units same as value.
Profit = Revenue - Cost — basic computed field; units same as revenue and cost (e.g., ₹).
Key SQL syntaxes to remember:
SELECT col1, AGG(col2) FROM table WHERE condition GROUP BY col1 HAVING agg_condition ORDER BY col1;
SELECT col1, SUM(col2) AS total FROM table GROUP BY col1 WITH ROLLUP; (syntax varies by DBMS)
SELECT col1, SUM(col2) OVER (PARTITION BY col1 ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total FROM table; (window function syntax; portable across major DBMSs)
CASE WHEN condition THEN expr1 ELSE expr2 END — create conditional computed field.
Worked examples
Example 1 — Grouping and aggregation (numerical, step-by-step): Dataset Sales (fields: SaleID, Date, Region, Amount (₹)) rows:
- (1, 2026-10-01, North, 1200)
- (2, 2026-10-02, South, 800)
- (3, 2026-10-02, North, 1500)
- (4, 2026-10-03, South, 700)
- (5, 2026-10-03, East, 900)
Question: Compute total sales and average sale per Region.
Step 1: GROUP BY Region. Groups: North: rows 1 and 3; South: rows 2 and 4; East: row 5.
Step 2: For each group compute SUM(Amount) and AVG(Amount). North: SUM = 1200 + 1500 = 2700 ₹. COUNT = 2. AVG = 2700 ÷ 2 = 1350 ₹. South: SUM = 800 + 700 = 1500 ₹. COUNT = 2. AVG = 1500 ÷ 2 = 750 ₹. East: SUM = 900 ₹. COUNT = 1. AVG = 900 ÷ 1 = 900 ₹.
Final answer (table): Region | TotalSales (₹) | AvgSale (₹) North | 2700 | 1350 South | 1500 | 750 East | 900 | 900
Example 2 — WHERE vs HAVING and percent of total (algorithmic trace): Dataset same as Example 1. Requirement: Show Regions with TotalSales > 1000 and PercentOfTotal.
Step 1: Compute grand total: TOTAL = 2700 + 1500 + 900 = 5100 ₹. Step 2: Compute group totals (from Example 1). Step 3: Filter groups where TotalSales > 1000 (HAVING used because we filter after aggregation): Regions North (2700) and South (1500) pass; East (900) excluded. Step 4: Compute % of total: North = (2700 / 5100) × 100 = 52.941...% → 52.94%. South = (1500 / 5100) × 100 = 29.411...% → 29.41%.
SQL to produce this (exam-style answer):
SELECT Region, SUM(Amount) AS TotalSales, (SUM(Amount) * 100.0 / (SELECT SUM(Amount) FROM Sales)) AS PercentOfTotal FROM Sales GROUP BY Region HAVING SUM(Amount) > 1000;
Example 3 — Calculated column and running total (window function trace): Dataset extended with a Cost column (₹):
- (1, 2026-10-01, North, Revenue=1200, Cost=800)
- (3, 2026-10-02, North, Revenue=1500, Cost=1000)
- (2, 2026-10-02, South, Revenue=800, Cost=600)
- (4, 2026-10-03, South, Revenue=700, Cost=400)
- (5, 2026-10-03, East, Revenue=900, Cost=500)
Task: For North region, compute Profit and running total of Profit ordered by Date.
Profit = Revenue - Cost. North rows ordered by Date: 2026-10-01 (Profit = 1200-800 = 400), 2026-10-02 (Profit = 1500-1000 = 500). RunningTotal after first row = 400. After second row = 400 + 500 = 900.
SQL (window function): SELECT Date, Revenue, Cost, (Revenue - Cost) AS Profit, SUM(Revenue - Cost) OVER (PARTITION BY Region ORDER BY Date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS RunningProfit FROM Sales WHERE Region = 'North' ORDER BY Date;
Output (North): Date | Profit (₹) | RunningProfit (₹) 2026-10-01 | 400 | 400 2026-10-02 | 500 | 900
Common mistakes
- Confusing COUNT(*) versus COUNT(column) with NULLs.
- Forgetting to compute grand total before percent-of-total (or doing it per-group incorrectly).
- Assuming window function results collapse rows — window functions keep original row count; aggregation reduces rows to groups.
- Double-counting when joining dimension tables before grouping: join multiplicity may inflate sums unless grouped correctly or aggregated on keys.
- Not specifying ORDER BY in the OVER() clause when a running total is intended.
For your exam
Question types you will see:
- Definitions and short notes: define filtering, grouping, aggregate functions, HAVING vs WHERE.
- Derivations / order-of-operations: explain SQL processing order and why HAVING is needed.
- Query writing: given a table, write SQL to produce grouped aggregates, percent-of-total, rollups or running totals.
- Numerical: hand-calculate grouped sums, averages, counts and percentages from a small dataset.
- Diagram question: draw grouping operation and label groups and aggregated output.
What full-marks answers include:
- Precise definitions using bold terms.
- Correct SQL or pseudo-SQL with GROUP BY and HAVING where needed.
- Clear step-by-step calculation shown for numerical answers (show intermediate sums, counts, totals, units like ₹).
- For diagrams, a labelled input table, grouping arrows or boxes, and final aggregated table.
- Note about NULL handling or DBMS-specific syntax if relevant.
Quick check
Q1: Which clause filters rows before aggregation, WHERE or HAVING?
Q2: How do you compute percent of total for a group's sum? Write the formula.
Q3: Does COUNT(column) include NULL values?
Answers: WHERE; % of total = (SUM(group) ÷ SUM(all)) × 100; No, COUNT(column) ignores NULLs.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?