Reference > Functions > Aggregate

Max

  • 2005, 2008, 2012, 2014, 2016, 2017, 2019, 2022

Max (Maximum) Aggregate Function

Use the Max function to return the maximum value from a set of values.

Syntax

db.fx.Max({expression})[.Distinct()]

Arguments

expression
The field expression, composite element, or function result to use in finding the maximum value.
Distinct()
Has no functional purpose and is provided for ISO compatibility only.

Returns

The same type as the generic parameter of the provided `expression`.

Examples

Select Statement

Select the maximum of total purchase amount for all purchases.

double minSales = db.SelectOne(
        db.fx.Max(dbo.Purchase.TotalPurchaseAmount)
    )
    .From(dbo.Purchase)
    .Execute();

Order By Clause

Select the maximum of total purchase amount for all purchases ordered by the maximum of total purchase amount descending.

IEnumerable<double> minSales = db.SelectMany(
        db.fx.Max(dbo.Purchase.TotalPurchaseAmount)
    )
    .From(dbo.Purchase)
    .OrderBy(db.fx.Max(dbo.Purchase.TotalPurchaseAmount).Desc())
    .Execute();

Having Clause

Select the maximum of total purchase amount for all purchases (ignoring null values), grouped by payment method type having an maximum greater than 10 and ordered by the maximum of total purchase amount.

IEnumerable<double> minSales = db.SelectMany(
        db.fx.Max(dbo.Purchase.TotalPurchaseAmount)
    )
    .From(dbo.Purchase)
    .GroupBy(dbo.Purchase.PaymentMethodType)
    .Having(db.fx.Max(dbo.Purchase.TotalPurchaseAmount) > 10)
    .OrderBy(db.fx.Max(dbo.Purchase.TotalPurchaseAmount))
    .Execute();
Previous
Count

© 2024 dbExpression. All rights reserved.