Skip to content

Commit

Permalink
Add example for groupBy (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosh committed Jun 25, 2022
1 parent 2a31fe9 commit df1a271
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Rel8/Table/Aggregate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ import Rel8.Type.Eq ( DBEq )

-- | Group equal tables together. This works by aggregating each column in the
-- given table with 'groupByExpr'.
--
-- For example, if we have a table of items, we could group the items by the
-- order they belong to:
--
-- @
-- itemsByOrder :: Query (OrderId Expr, ListTable Expr (Item Expr))
-- itemsByOrder = aggregate $ do
-- item <- each itemSchema
-- let orderId = groupBy (itemOrderId item)
-- let orderItems = listAgg item
-- pure (orderId, orderItems)
-- @
groupBy :: forall exprs aggregates. (EqTable exprs, Aggregates aggregates exprs)
=> exprs -> aggregates
groupBy = fromColumns . hgroupBy (eqTable @exprs) . toColumns
Expand Down

0 comments on commit df1a271

Please sign in to comment.