cycquery.ops.GroupByAggregateΒΆ

class GroupByAggregate(groupby_cols, aggfuncs, aggseps=None)[source]ΒΆ

Bases: QueryOp

Aggregate over a group by object.

Parameters:
  • groupby_cols (Union[str, List[str]]) – Columns by which to group.

  • aggfuncs (Union[Dict[str, Sequence[str]], Dict[str, str]]) – Specify a dictionary of key-value pairs: column name: aggfunc string or column name: (aggfunc string, new column label) This labelling prevents the aggregation of the same column using multiple aggregation functions.

  • aggseps (Optional[Dict[str, str]]) – Specify a dictionary of key-value pairs: column name: string_aggfunc separator If string_agg used as aggfunc for a column, then a separator must be provided for the same column.

Examples

>>> GroupByAggregate("person_id", {"person_id": "count"})(table)
>>> GroupByAggregate("person_id", {"person_id": ("count", "visit_count")})(table)
>>> GroupByAggregate("person_id", {"lab_name": "string_agg"}, {"lab_name": ", "})(table)
>>> GroupByAggregate("person_id", {"lab_name": ("string_agg", "lab_name_agg"}, {"lab_name": ", "})(table)

Methods

__call__(table)[source]ΒΆ

Process the table.

Parameters:

table (Union[Select, Subquery, Table, DBTable]) – Table on which to perform the operation.

Returns:

Processed table.

Return type:

sqlalchemy.sql.selectable.Subquery