on da76712 help sign in
rel8/src/Rel8/Query/Distinct.hs
40 lines 1.4 kB history raw
1module Rel8.Query.Distinct
2 ( distinct
3 , distinctOn
4 , distinctOnBy
5 )
6where
8-- base
9import Prelude ()
11-- opaleye
12import qualified Opaleye.Distinct as Opaleye
13import qualified Opaleye.Order as Opaleye
15-- rel8
16import Rel8.Order ( Order( Order ) )
17import Rel8.Query ( Query )
18import Rel8.Query.Opaleye ( mapOpaleye )
19import Rel8.Table.Eq ( EqTable )
20import Rel8.Table.Opaleye ( distinctspec, unpackspec )
23-- | Select all distinct rows from a query, removing duplicates. @distinct q@
24-- is equivalent to the SQL statement @SELECT DISTINCT q@.
25distinct :: EqTable a => Query a -> Query a
26distinct = mapOpaleye (Opaleye.distinctExplicit distinctspec)
29-- | Select all distinct rows from a query, where rows are equivalent according
30-- to a projection. If multiple rows have the same projection, it is
31-- unspecified which row will be returned. If this matters, use 'distinctOnBy'.
32distinctOn :: EqTable b => (a -> b) -> Query a -> Query a
33distinctOn proj = mapOpaleye (Opaleye.distinctOnExplicit unpackspec proj)
36-- | Select all distinct rows from a query, where rows are equivalent according
37-- to a projection. If there are multiple rows with the same projection, the
38-- first row according to the specified 'Order' will be returned.
39distinctOnBy :: EqTable b => (a -> b) -> Order a -> Query a -> Query a
40distinctOnBy proj (Order order) = mapOpaleye (Opaleye.distinctOnByExplicit unpackspec proj order)