Use aggregate functions, like counting rows

Use functions like
count :: Expr a -> Expr Int
_sum, _max, avg :: Num a => Expr a -> Expr a

Aggregate functions can only be used in a project. We automatically group on all other fields in the project.
You need a field of the return type to put the value into in your project


aggrSum =
  withDB $ \ db ->
  do res <- query db $
            do farms <- table farms
               project (H.farm_id << _sum
                         (fromNull (constant 0) (farms!chickens) ))
     putStrLn "Number of chickens on all farms:"
     mapM (putStrLn.showKO) res
  where showKO r = show (r!H.farm_id)


Frame> aggrSum
Number of chickens on all farms:
221

If you need for example a field with an Int value but don't have one available, you can use a hack to create one.


module NewFieldDef where import Database.HaskellDB.DBLayout data NewField = NewField instance FieldTag NewField where fieldName _ = "newField" newTbl :: Table ( RecCons NewField (Expr Int) RecNil) newTbl = baseTable "newTbl" $ hdbMakeEntry NewField newField :: Attr NewField Int newField = mkAttr NewField
Then you can import NewFieldDef and use newField as an int field.

Maybe you get an error like this one:

aggr =
  withDB $ \ db ->
  do res <- query db $
            do t <- table H.farms
               project (H.chickens << _sum (t!H.chickens))
     return res
     


Bad2> aggr
ERROR "Bad2.hs":6 - Unresolved top-level overloading
*** Binding : aggr
*** Outstanding context : Num (Maybe Int)
ERROR - Undefined variable "aggr"

Then you have to use fromNull to remove the maybe, and put the result into a field wich is not null.

Here are all words and functions with references, in alphabetical order

Go to main page