↓ Skip to main content
A pen and ink drawing of a wooden sorting tray on a workbench with seven compartments, each holding a stack of orange tokens, from tall on the left to short on the right. A teal band wraps two neighbouring stacks together. A brass tally counter with a blank dial stands to the left, and in front of the tray a teal dish holds one token of each kind in a row.

Counts, Aggregates and Distinct Values in the Maximo REST API

The Maximo REST API in Practice - This article is part of a series.
Part 4: This Article
The where clause picks the records, oslc.select shapes them, and sorting and paging walks through them. Often you only need a number: how many, how much, which values exist. The REST API can answer that in one request and leave the records in the database.

The rules here come from IBM’s documentation and from our own work with the REST API, on Manage in the MAS 9.2 series. We ran every request on our test system, against IBM’s demo data, as a user without administrator rights. The examples read the object structure MXAPIASSET at the site BEDFORD, and the work orders through MXAPIWODETAIL.

There are four forms, and each one answers a different question:

Pick the form by the question you ask Pick the form by the question you ask
Pick the form by the question you ask

How many records: count=1
#

count=1 returns the number of records that match the where clause, and nothing else:

oslc.where=siteid="BEDFORD" and status="OPERATING"
count=1
{ "totalCount": 199 }

The database counts the rows. No record is read, so this is the cheapest request in the API. It fits a dashboard tile, a check before an export, or a test that a filter works.

The total with every page
#

When you read records page by page and also want the total, add collectioncount=1. responseInfo then carries totalCount and totalPages:

"responseInfo": {
  "totalCount": "…",
  "totalPages": "…",
  "pagenum": 1,
  "nextPage": { "href": "…" }
}

An administrator can switch the totals on for every request with the system property mxe.oslc.collectioncount. On our test system it was on, so the totals came without the parameter. Send collectioncount=1 anyway when your client needs the total, and the request works on every system.

A number for each record
#

An aggregate in oslc.select computes a number over a relationship of each record. Asking for the open work orders of an asset, and their priorities:

oslc.where=siteid="BEDFORD" and assetnum="11430"
oslc.select=assetnum,openwo._dbcount,openwo.wopriority._dbmax
{
  "assetnum": "11430",
  "openwo": {
    "_dbcount": 43,
    "wopriority": { "_dbmax": 8 }
  }
}

The database computes the values. The work orders themselves never reach the answer. Six functions exist:

TermReturns
<rel>._dbcountthe number of related records
<rel>._existstrue or false: does the relationship have a record
<rel>.<attr>._dbsumthe sum of the attribute
<rel>.<attr>._dbavgthe average of the attribute, see below
<rel>.<attr>._dbmaxthe largest value
<rel>.<attr>._dbminthe smallest value

A few rules decide whether the request works:

  • The result is nested. Each relationship becomes an object, and each attribute an object inside it, as in the JSON above.
  • An alias makes it flat. openwo._dbcount--wocount returns "wocount": 43 at the top level of the record. For a client this is the easier form to read.
  • Sum, average, maximum and minimum need a number. openwo.wonum._dbmax is refused with BMXAA8882E, because wonum is a text attribute.
  • One relationship deep for the attribute functions. location.locoper._dbcount counts across two relationships. location.locoper.locpriority._dbsum is refused with oslc#invaliddbstat.

In a list, every record gets its own numbers. This returns each asset with the count of its open work orders:

oslc.where=siteid="BEDFORD"
oslc.select=assetnum,description,openwo._dbcount--wocount
oslc.orderBy=+assetnum
oslc.pageSize=20

The average counts the records without a value
#

_dbavg is the sum divided by the count of all related records, and that count includes the records where the attribute is empty. We measured it on an asset whose open work orders include task rows without a priority: _dbavg equalled _dbsum divided by _dbcount, empty rows included.

An example with three work orders makes the difference clear:

Work orderPriority
12
24
3empty

_dbavg returns 2: the sum 6 divided by three records. The average most people expect is 3, the sum divided by the two records that have a priority. The group by average in the section below returns 3. When empty values are common, compute the average yourself from _dbsum and a count of the records that have a value.

Aggregates in the where clause
#

The same terms filter records. This returns the assets with more than three open work orders:

oslc.where=siteid="BEDFORD" and openwo._dbcount>3
oslc.select=assetnum,openwo._dbcount--wocount

_dbsum, _dbavg, _dbmax and _dbmin work the same way, for example openwo.wopriority._dbmax>=3. To test whether a relationship has records, the where clause uses _dbexists:

oslc.where=siteid="BEDFORD" and openwo._dbexists=0
count=1

This counts the assets without an open work order. Note the two names: _dbexists in the where clause, _exists in the select clause. openwo._dbexists in oslc.select is refused with BMXAA4176E, “Attribute _DBEXISTS does not exist”. The where clause article has more on filtering by a relationship.

Which values exist: distinct
#

distinct takes the place of oslc.select and returns the values of one attribute, each value once:

oslc.where=siteid="BEDFORD"
distinct=status
["ACTIVE", "DECOMMISSIONED", "NOT READY", "OPERATING"]

The answer is a plain JSON array, without member and without responseInfo. Three details:

  • An empty value is part of the list. distinct=serialnum returns null as one of the values. A client that fills a filter list from it has to skip or label that entry.
  • oslc.select and oslc.pageSize are ignored. The array holds every value in one answer.
  • Name one attribute. distinct=status,priority returns a status for each distinct pair of status and priority, so the list repeats the same status several times. For combinations, use group by.

An unknown attribute is refused with BMXAA10000E, “Invalid attribute nosuchattr specified for distinct clause”.

Numbers per group: gbcols
#

Group by returns one row per group, with the aggregates you ask for. gbcols names the grouping attributes and the functions:

oslc.where=siteid="BEDFORD"
gbcols=status,count.*
[
  {
    "status": "ACTIVE",
    "status_description": "Active",
    "count": 40,
    "collectionref": "…"
  },
  { "status": "DECOMMISSIONED", "…": "…" }
]

oslc.where filters the records before they are grouped, and a saved query works too. The answer is a JSON array, one entry per group. An attribute with a domain also gets its description, as status_description above.

The functions are written as <function>.<attribute>, and the result is named <function>_<attribute>:

In gbcolsIn the answer
count.*count, the number of records in the group
count.prioritycount_priority, the records with a priority
sum.prioritysum_priority
avg.priorityavg_priority, the average of the records with a value
max.priority, min.prioritymax_priority, min_priority

A group whose records all have an empty value gets null for the function. Several grouping attributes give one row per combination: gbcols=siteid,status,count.* counts the assets per site and status. A list of only functions, such as gbcols=count.*,max.priority, gives one row with the totals over all matching records.

Sorting the groups
#

gbsortby sorts the rows, with the same sign rules as oslc.orderBy:

gbcols=siteid,count.*
gbsortby=-count.*

This lists the sites with the most assets first. The + must be encoded as %2B here too. A raw + is refused with BMXAA9757E, a general message that names no cause.

The link of a group#

Each row carries a collectionref that should lead to the records of the group. On our test system that link carried two question marks, …/mxapiasset?&lean=1?oslc.where=…, and following it did not return the records of the group. Build the link from the row instead: the request’s own where clause, plus the group’s values.

oslc.where=siteid="BEDFORD" and status="ACTIVE"

Ranges: gbrange
#

gbrange merges values into ranges, and counts each range. A list of values goes in square brackets with a colon:

oslc.where=siteid="BEDFORD"
gbcols=status,count.*
gbrange=status={[OPERATING:NOT READY]}

OPERATING and NOT READY become one row, with "status": ["OPERATING", "NOT READY"] and their summed count. The other values stay rows of their own. Number ranges use .., with [ and ] for an included bound and ( and ) for an excluded one:

gbcols=priority,count.*
gbrange=priority={[1..3],[4..7)}

This returns one row for priority 1 to 3, one for 4 to 6, and the values outside the ranges, such as an empty priority, as rows of their own. A range needs count.* in gbcols. Without it, the request is refused with BMXAA9757E.

Which form for which job
#

JobForm
A dashboard tile with one numbercount=1
The total next to a paged listcollectioncount=1
A column with the open work orders per assetoslc.select with openwo._dbcount--wocount
Only the assets with open work ordersoslc.where with openwo._dbexists=1
The values for a filter listdistinct
A chart: records per status, per sitegbcols with count.*
A chart with buckets, such as priority bandsgbcols with gbrange

Errors at a glance
#

RequestAnswerFix
openwo.wonum._dbmax400 BMXAA8882Euse a numeric attribute
location.locoper.locpriority._dbsum400 oslc#invaliddbstatone relationship deep
openwo._dbexists in oslc.select400 BMXAA4176Euse openwo._exists
distinct=nosuchattr400 BMXAA10000Euse an attribute of the main object
gbcols= empty400 BMXAA9307Ename at least one column
gbcols=nosuchattr,count.*400 BMXAA9757Echeck every name in gbcols
gbsortby with a raw +400 BMXAA9757Eencode it as %2B
gbrange without count.*400 BMXAA9757Eadd count.* to gbcols

BMXAA9757E says only “Error executing the request”. It stands for several causes, so check the group by parameters one by one.

Examples
#

Each request below returned data on our test system. Each line is one query parameter.

Assets with open work orders, and how many each has:

oslc.where=siteid="BEDFORD" and openwo._dbexists=1
oslc.select=assetnum,openwo._dbcount--wocount
oslc.orderBy=+assetnum
oslc.pageSize=20

The planned hours of the work orders per status:

/api/os/mxapiwodetail
oslc.where=siteid="BEDFORD"
gbcols=status,count.*,sum.estdur
gbsortby=-count.*

The statuses in use at a site, for a filter list:

oslc.where=siteid="BEDFORD"
distinct=status

Assets per priority band:

oslc.where=siteid="BEDFORD"
gbcols=priority,count.*
gbrange=priority={[1..3],[4..7)}

What comes next
#

IBM’s own pages are the reference to keep next to this article:

  • Selecting in the IBM Maximo Manage documentation, with the aggregation functions.
  • Selecting distinct data in the same documentation.
  • Aggregation in IBM’s Maximo REST API guide on GitHub, the only IBM page on group by.

The next article of the series covers saved queries. The series overview lists all parts. If you want help with the REST API in your own Manage, our services page shows how we work, or get in touch.

IBM and Maximo are trademarks of International Business Machines Corporation, registered in many jurisdictions worldwide.

The Maximo REST API in Practice - This article is part of a series.
Part 4: This Article