Blog

Find Unique Values

Here is a background script you can use to find unique values.  You can use it in scripts - background or alter slightly to use a script include for a report.  

In the example below, it finds all the unique Operating Systems used in the Computer table:

Background Script

findUnique('cmdb_ci_computer','os');//put the table and field you want to find unique
function findUnique(table,field) {
var au = new ArrayUtil();
var uniqueArray = [];
var gr = new GlideRecord(table);
gr.orderBy(field);
gr.addNotNullQuery(field);
gr.query();
while (gr.next()) {
uniqueArray.push(gr[field].toString());
}
//gs.print('All Values: ' +uniqueArray);
gs.print('Unique Values: ' +au.unique(uniqueArray));
//return au.unique(uniqueArray);
}

Results

*** Script: Unique Values: AIX,HP/UX,Linux Red Hat,Mac OS 10 (OS/X),OS/400,Solaris,Windows 2000,Windows 2000 Advanced Server,Windows 2000 Datacenter Server,Windows 2000 Professional,Windows 2000 Server,Windows 2003 Standard,Windows 95,Windows NT 4.0,Windows XP,Windows XP Professional