Creating aggregate queries

Aggregate queries are used to group multiple rows of data based on certain criteria and calculate a single meaningful value from the grouped data. Common aggregate functions include average, sum, count, and others. These queries are frequently used to retrieve data for key performance indicators (KPI) cards, bar charts, and line charts.

About this task

While typical queries return a value or multiple values across rows based on given criteria, aggregate queries provide a single value or grouped values. For instance, to find the count of delayed order lines, a simple query is sufficient. However, to calculate the total value of goods that are associated with these delayed order lines, an aggregate function such as sum is necessary because it combines values across multiple rows into a single result.

Note: The query builder does not support aggregation, so custom code is required.

Before you begin

Create a query. For more information, see Adding queries to cards.

Procedure

  1. In the Code editor, write an aggregation function that is similar to the functions in the KPI card example and Bar chart example.
  2. Run the query.

KPI card example

The following card shows the total value of order lines with late planned delivery. This card uses a simple query to identify the number of order lines with late planned delivery and uses aggregate function to get the value of order lines with late planned delivery.

Figure 1. Order lines with late planned delivery
A KPI card shows 7 order lines with late planned delivery and a value of $247,170.
query latePlannedDeliveryMetrics($tenantId: String, $dateRangeStart: String = "1970-01-01T00:00:00Z", $dateRangeEnd: String = "2970-01-01T00:00:00Z", $categoryFilter: BooleanExp = {CONSTANT_VALUE: true}, $supplierFilter: BooleanExp = {CONSTANT_VALUE: true}, $locationFilter: BooleanExp = {CONSTANT_VALUE: true}) {

  totalLatePlannedDelivery: businessObjects(

    advancedFilter: {AND: [{GREATER_THAN: [{SELECT: "plannedDeliveryDelay", type: FLOAT}, {VALUE: "0.0", type: FLOAT}]}, {GREATER_EQUALS: [{SELECT: "order.createdDate"}, {VALUE: $dateRangeStart}]}, {LESS_EQUALS: [{SELECT: "order.createdDate"}, {VALUE: $dateRangeEnd}]}, {NOT: {EQUALS: [{SELECT: "deliveryStatusByDate", type: STRING}, {type: STRING, VALUE: "FULLY_DELIVERED"}]}}, {EQUALS: [{SELECT: "order.orderType", type: STRING}, {type: STRING, VALUE: "INBOUND"}]}, $categoryFilter, $supplierFilter, $locationFilter]}

    cursorParams: {first: 0}

    hint: {viewId: "graph"}

  simpleFilter: {tenantId: $tenantId, type: OrderLine}

    aggregationParams: {reduce: {expression: {fieldPath: "value", operator: SUM, type: INT}, output: {resultAlias: "totalOrderLinesValue", type: INT}}}

  

  ) {

    totalCount

    aggregationResults {

     

      values {

        name

        numValue

      }

    }

    edges {

      cursor

      object {

        ... on OrderLine {

          value

        }

      }

    }

  }

}
An aggregation function is defined in aggregationParams. The key parameters in aggregationParams are expression, reduce, fieldPath, and output.
reduce
Specifies which fields to aggregate and the operation, such as sum, average, or count, to perform on those fields.
fieldPath
Specifies the attribute to aggregate. In this example, the attribute is value. The operator defines the aggregation operation to perform. The values can be COUNT, COUNT_DISTINCT, SUM, MAX, MIN, or AVG.
output
Specifies the result or final aggregated value after the defined operation is applied. The name of the result in this example is totalOrderLinesValue.
expression
Includes fieldpath, operator, and output.
aggregationResults
Defines the result of the aggregation. The values array within aggregationResults stores the name and numValue. The name refers to the output name of the aggregation, while numValue represents the aggregated result or value. In this example, the name is totalOrderLinesValue and numValue is 247170.

Bar chart example

The following chart shows the customers who spent the most money during the defined period. The query of this chart retrieves a list of order lines that are grouped by buyer and aggregated by value. Also, it allows customization of the date range and other parameters by using variables.

Figure 2. Top customers by value
A bar chart that displays a bar for each customer to show the amount spent.
query ($dateRangeStart: String = "1970-01-01T00:00:00Z", $dateRangeEnd: String = "2970-01-01T00:00:00Z") {

  Query1: businessObjects(

    hint: {viewId: "graph"}

    cursorParams: {first: 20}

    simpleFilter: {type: OrderLine, tenantId: "fcc67ac0-629f-4fbd-a747-1a95885e3c2e"}

    advancedFilter: {AND: [{EQUALS: [{SELECT: "order.orderType"}, {VALUE: "OUTBOUND"}]}, {GREATER_EQUALS: [{SELECT: "plannedShipDate"}, {VALUE: $dateRangeStart}]}, {LESS_THAN: [{SELECT: "plannedShipDate"}, {VALUE: $dateRangeEnd}]}]}

    aggregationParams: { dimension: {fieldPath: "order.buyer.name", type: STRING}, reduce: {expression: {fieldPath: "value", operator: SUM, type: INT},

 output: {resultAlias: "abc", type: INT}},paginationParams: {first: 10, sort: {fieldPath: "abc", order: DESC}}}

  ) {

    aggregationResultsTotalCount

    aggregationResults {

      dimensionValues {

        name

        value

        __typename

      }

      values {

        name

        numValue

        __typename

      }

      __typename

    }

    __typename

    edges {

      object {

        ... on OrderLine {

          value

          order {

            buyer {

              name

            }

          }

        }

      }

    }

  }

}

The reduce, fieldpath, operator, output, and expression parameters are similar to the KPI card example. The following additional parameters are used in this example:

dimension
Defines the x-axis, which means it specifies the attribute by which data is grouped. For example, order.buyer.name.
PaginationParams
Defines how the output is visually displayed, which is the number of results that are shown in the chart, typically stored in the list. In this example, the top 10 results are displayed. The output is selected by sorting, which defines the order of the output, which can be ascending or descending.
aggregationResults
Defines the result of the aggregation. In this example, DimensionValues stores the name of the grouped attribute. For example, F & A Metals and SilverOptics.

What to do next

A course is available to learn more about simple and aggregate queries. For more information, see Supply Chain App Studio: Builders for Dashboards and Workqueues.