Skip to content

Query

Defines a query to retrieve data.

Remarks

Property names are case-insensitive. Criteria are combined with And unless a criteria specifies Condition : 'Or'; use Blocks to group conditions, such as (A OR B) AND C.

Columns, criteria and orders are defined with expressions, such as Customer.Name, Quantity * UnitPrice, Sum(Amount) or DateTrunc('Month', OrderDate). Joins and grouping are created from the expressions; see QueryColumn.Expression for the syntax.

Examples

Query active records with optional filters

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
var table = $Database.Get({
    TargetSchema : 'Sales',
    TargetTable : 'Customers',
    Columns : [
        { Name : 'Id' },
        { Name : 'Name' },
        { Name : 'City' }
    ],
    Where : {
        Criteria : [
            { Expression : 'Active', Value : true },
            // ignored when the City field of the form is empty
            { Expression : 'City', Value : $Xml.Evaluate('Filter/City'), IgnoredValues : [''] },
            { Expression : 'Name', Value : $Xml.Evaluate('Filter/Name'), Comparison : 'Like', IgnoredValues : [''] }
        ]
    },
    Order : [
        { Expression : 'Name', Type : 'Ascending' }
    ],
    Start : 0,
    MaxLength : 50
});

Group conditions with blocks

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// (Status = 'Open' OR Status = 'Pending') AND Amount > 1000
var table = $Database.Get({
    TargetSchema : 'Sales',
    TargetTable : 'Orders',
    Where : {
        Blocks : [{
            Criteria : [
                { Expression : 'Status', Value : 'Open', Condition : 'Or' },
                { Expression : 'Status', Value : 'Pending' }
            ]
        }],
        Criteria : [
            { Expression : 'Amount', Value : 1000, Comparison : 'GreaterThan' }
        ]
    }
});

Total sales per customer in the last 30 days

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
var totals = $Database.Get({
    TargetSchema : 'Sales',
    TargetTable : 'Orders',
    Columns : [
        { Name : 'Customer', Expression : 'Customer.Name' },
        { Name : 'Total', Expression : 'Sum(Quantity * UnitPrice)' },
        { Name : 'OrderCount', Expression : 'Count(Id)' }
    ],
    Where : {
        Criteria : [
            { Expression : 'OrderDate', Value : $Calendar.AddDays($Calendar.Today, -30), Comparison : 'GreaterThanOrEqualTo' },
            // filters the groups: customers with a total over 10000
            { Expression : 'Sum(Quantity * UnitPrice)', Value : 10000, Comparison : 'GreaterThan' }
        ]
    },
    Order : [
        { Expression : 'Sum(Quantity * UnitPrice)', Type : 'Descending' }
    ]
});

Monthly summary with date functions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
var months = $Database.Get({
    TargetSchema : 'Sales',
    TargetTable : 'Orders',
    Columns : [
        { Name : 'Month', Expression : "DateTrunc('Month', OrderDate)" },
        { Name : 'AverageDeliveryDays', Expression : "Avg(DateDiff('Day', OrderDate, ShipDate))" }
    ],
    Where : {
        Criteria : [
            { Expression : "DatePart('Year', OrderDate)", Value : 2024 }
        ]
    },
    Order : [
        { Expression : "DateTrunc('Month', OrderDate)" }
    ]
});

Compare two columns

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// orders shipped after their due date
var lateOrders = $Database.Get({
    TargetSchema : 'Sales',
    TargetTable : 'Orders',
    Where : {
        Criteria : [
            { Expression : 'ShipDate', Value : 'DueDate', ValueType : 'Expression', Comparison : 'GreaterThan' }
        ]
    }
});

Capabilities

Definition

Properties

Parameters