Closed TomMD closed 3 years ago
Figured it out, though I feel it is a bit unintuitive. Probably just tired.
peopleWithMoreThanTwoPets <- query $ do
person <- select people
restrict (person ! #name `isIn` do
(c:*:o) <- aggregate (do
ownerPet <- distinct (select pets)
owner <- groupBy (ownerPet ! #owner)
pure (count (ownerPet ! #pet) :*: owner)
)
restrict (c .> 2)
pure o)
pure person
I'm probably burned out after a long week, but am having serious trouble seeing how the library allows for filtering based on distinct fields of a count.
For example (full code below). Say you have two tables of
people
andpets
(foreign key to people). You want all people who own more than two types of pets. Thus, you must group by people, count the number of distinct pets and restrict the people to just those rows. Writing the actual query has eluded me, is there something I've entirely overlooked?