GlideAggregate COUNT on a reference field returns 1 instead of the number of distinct records

seungchulkim
Tera Contributor

Hi,

You can fix this by using COUNT(DISTINCT instead of COUNT with a field name. addAggregate('COUNT', 'field') does not count distinct values, so a query like “how many servers have this software installed” comes back as 1 (or another wrong number) even when thousands of servers match.

Step 1: Find the existing aggregate

Find:

seungchulkim_0-1794391696801.png

Step 2: Replace it with COUNT(DISTINCT

Replace with:

seungchulkim_1-1794391724086.png

Note that the same string 'COUNT(DISTINCT' (with the open parenthesis, no closing one) must be used in both addAggregate and getAggregate.

Step 3 (optional): Get rows and distinct counts in one query

seungchulkim_2-1794391761852.png

 

Result: in our case the detail screen showed “1 server” for a package that was actually installed on more than 12,000 servers. After the change, the count matched the list view.

If this helped, please mark it as Helpful 👍



0 REPLIES 0