Solve a mix of coding and SQL questions covering core operations like group by, having, rank, CTEs, and distinct.
Implement the coding portion using the function below. Aggregate records by (category, group), count distinct members, sum values, filter groups by minimum_total, rank qualifying groups within each category, and keep the top top_n groups per category. Sort results by category, rank, and group.
rows is a list of dictionaries with string keys category, group, member, and integer value. Return a list of dictionaries containing category, group, distinct_members, total_value, and rank.
def analyze_records(rows, minimum_total, top_n):