Group sum aggregation

Write codeEasy20 minSnowflake and Databricks style

Your task

Given a list of records (each a dict), a group_key, and a numeric value_key, compute the SUM of value_key for each distinct value of group_key, the equivalent of SELECT group_key, SUM(value_key) GROUP BY group_key. Return a list of [group_value, total] pairs sorted alphabetically by group value. If a total is integral, return it as an int rather than a float.

Examples

group_totals(rows: list[dict], group_key: str, value_key: str) -> list[list]
Example 1
Inputgroup_totals([{"region": "west", "sales": 10}, {"region": "east", "sales": 5}, {"region": "west", "sales": 3}], "region", "sales")
Output[["east", 5], ["west", 13]]