KatInterview

Conditional Sum Aggregation with Group Filtering

SQL & DataAuto-graded · 18 test cases
MediumAuto-gradedSQL & Dataconditional-aggregationgroupingsimulation

Last updated

Compute filtered totals two ways and group-then-threshold results, mirroring SQL conditional aggregation semantics.

You are given a list of transaction records. Each record is a dictionary with three keys:

  • "group" — a string identifying the group (e.g. a merchant or region).
  • "amount" — an integer representing the transaction value.
  • "qualifies" — a boolean indicating whether this transaction should be counted toward the conditional total.

You are also given an integer minimum_group_total.

Your function must return a dictionary with exactly three keys:

  1. "where_total" — the sum of amount over every record whose qualifies is True. This mirrors a SQL query that filters rows with a WHERE clause before aggregating.
  2. "case_total" — the sum of amount for every record, treating amount as 0 when qualifies is False. This mirrors SUM(CASE WHEN qualifies THEN amount ELSE 0 END) applied to all rows without a WHERE filter. Both totals will be numerically equal, but the approaches differ conceptually.
  3. "groups" — a dictionary mapping each distinct group name to the sum of amount values from that group's qualifying records only. Groups with no qualifying records must still appear with a value of 0. After computing the per-group totals, keep only the groups whose total is greater than or equal to minimum_group_total. This mirrors a SQL GROUP BY followed by a HAVING clause on the conditional aggregate.

Return the dictionary with keys "where_total", "case_total", and "groups".

Constraints

  • 0 <= len(rows) <= 10^5
  • Each record always contains all three keys: "group", "amount", and "qualifies".
  • amount is an integer in the range [-10^6, 10^6].
  • qualifies is a boolean (True or False).
  • "group" is a non-empty string.
  • minimum_group_total is an integer in the range [-10^9, 10^9].
  • An empty input list yields 0 for both totals and an empty dictionary for "groups".
  • Groups whose qualifying total is less than minimum_group_total are excluded from "groups". Groups that never appear in the input do not appear in "groups".
  • The order of keys in the returned dictionaries does not matter.

Examples

Example 1

Input: conditional_aggregation([{'group': 'A', 'amount': 100, 'qualifies': True}, {'group': 'A', 'amount': 40, 'qualifies': False}, {'group': 'B', 'amount': 75, 'qualifies': True}], 50)

Output: {'where_total': 175, 'case_total': 175, 'groups': {'A': 100, 'B': 75}}

Example 2

Input: conditional_aggregation([{'group': 'north', 'amount': 10, 'qualifies': True}, {'group': 'north', 'amount': 20, 'qualifies': True}, {'group': 'south', 'amount': 100, 'qualifies': False}], 25)

Output: {'where_total': 30, 'case_total': 30, 'groups': {'north': 30}}

Example 3

Input: conditional_aggregation([{'group': 'A', 'amount': 5, 'qualifies': False}, {'group': 'B', 'amount': 8, 'qualifies': False}], 0)

Output: {'where_total': 0, 'case_total': 0, 'groups': {'A': 0, 'B': 0}}

Hints

Hint 1

Both the WHERE-style and CASE-WHEN-style totals produce the same number; the difference only becomes visible when grouping, because WHERE drops groups with no qualifying rows while CASE WHEN retains them with a zero total.

Hint 2

Initialize every group's running total to 0 before iterating over its records, so groups whose records never qualify still appear in the output with a value of 0.

Hint 3

Apply the minimum threshold only after all records have been processed, analogous to HAVING running after GROUP BY in SQL's logical execution order.