Reference > Functions > Mathematical

Abs

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

Abs (Absolute) Mathematical Function

Use the Abs function to return the absolute value of the provided value.

Syntax

db.fx.Abs({expression})

Arguments

expression

Returns

The positive value of the provided `expression`.

Examples

Select Statement

Select the absolute value of total purchase amount for all purchases.

IEnumerable<double> value = db.SelectMany(
        db.fx.Abs(dbo.Purchase.TotalPurchaseAmount)
    )
    .From(dbo.Purchase)
    .Execute();

Where Clause

Select the purchase ids of any purchase where the total purchase amount isn't the same (in this example, think of a refund entered as a purchase, where the total purchase amount would have a negative value).

IEnumerable<int> value = db.SelectMany(
        dbo.Purchase.Id
    )
    .From(dbo.Purchase)
    .Where(
        db.fx.Abs(dbo.Purchase.TotalPurchaseAmount) != dbo.Purchase.TotalPurchaseAmount
    )
    .Execute();

Order By Clause

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

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

Group By Clause

Select the payment method and absolute value of total purchase amount for all purchases, grouped by payment method type and ordered by the absolute value of total purchase amount.

IEnumerable<dynamic> values = db.SelectMany(
        dbo.Purchase.PaymentMethodType,
        db.fx.Abs(dbo.Purchase.TotalPurchaseAmount).As("TotalPurchaseAmount")
    )
    .From(dbo.Purchase)
    .GroupBy(
        dbo.Purchase.PaymentMethodType,
        db.fx.Abs(dbo.Purchase.TotalPurchaseAmount)
    )
    .OrderBy(db.fx.Abs(dbo.Purchase.TotalPurchaseAmount))
    .Execute();

Having Clause

Select the payment method and absolute value of total purchase amount for all purchases, grouped by payment method type having an absolute value greater than 10 and ordered by the absolute value of total purchase amount.

IEnumerable<dynamic> values = db.SelectMany(
        dbo.Purchase.PaymentMethodType,
        db.fx.Abs(dbo.Purchase.TotalPurchaseAmount).As("TotalPurchaseAmount")
    )
    .From(dbo.Purchase)
    .GroupBy(
        dbo.Purchase.PaymentMethodType,
        db.fx.Abs(dbo.Purchase.TotalPurchaseAmount)
    )
    .Having(db.fx.Abs(dbo.Purchase.TotalPurchaseAmount) > 10)
    .OrderBy(db.fx.Abs(dbo.Purchase.TotalPurchaseAmount))
    .Execute();
Previous
Mathematical

© 2024 dbExpression. All rights reserved.