Using an aggregate function as a column selection

The following Gosu code uses the Max database aggregate function. The query builder API appends a GROUP BY Country clause to the SQL query that the database receives.

uses gw.api.database.Query
uses gw.api.database.QuerySelectColumns
uses gw.api.path.Paths
uses gw.api.database.DBFunction

var query = Query.make(Address)
var latestAddress = QuerySelectColumns.dbFunctionWithAlias("LatestAddress", 
        DBFunction.Max(Paths.make(Address#CreateTime)))
var rowResults = query.select({
  QuerySelectColumns.pathWithAlias("Country", Paths.make(Address#Country)),
  latestAddress
})

The query returns the time of the most recent address creation for each country.

See also