what does groupBy() in glideAggregate exactly do?

Varun Sharma
Tera Expert

Hi Experts,

 

i was trying to clean up duplicate CI, and i found that my code works if i group By all three category (parent, child & type) and not if i just groupBy one category.  

May i ask what groupBy(), does here exactly? why is this code is not working without these groupings

var agg = new GlideAggregate('cmdb_rel_ci');
agg.addQuery(encodedQuery);
agg.setLimit(batch);

agg.groupBy('parent');
agg.groupBy('type');
agg.groupBy('child');

agg.addAggregate('COUNT');
agg.addHaving('COUNT', '>', '1'); // if Query finds more than 1 record with same parent, type and child
agg.query();

is the groupBy here is same as the screenshot in list view? 

if so how can we have group by different category ? 

find_real_file.png

I'm still a noob and trying to understand the platform. 

I'll be grateful if you can explain that.

 

Regards, 

Varun Sharma

1 ACCEPTED SOLUTION

Hi @Varun Sharma 

 

No, It will take all.

Example:

In my PDI, I have total 558 CIs.

agg.groupBy('parent'); // answer is 180
agg.groupBy('type'); // answer is 19
agg.groupBy('child'); // answer is 189

 

If I grouped all three fields.

Then i'm getting 558.

Here what happening is, If you have atleast one Parent or type or child. It will count.

 

If I grouped only one field(parent).

Then I'm getting 180.

Here what happening is, It will take only parent field which are not empty.

 

If my response is helpful, then Please mark as Correct Answer/Helpful.

Please check and let us know.

Thanks:)

Thanks,
Shakeel Shaik 🙂

View solution in original post

9 REPLIES 9

Rahul Dev1
Giga Expert

Hi Varun

Checkout this if it helps you:

find_real_file.png

 

Hi Rahul ,

 

I did go through with this. didn't understand it much, that's why i decided to post the question. 

here in code snippet you'll see the groupBy is used just once. 

in my code it's used thrice, i'm trying to understand how multiple groupby line by line impact the result.

 

Regards, 


Varun Sharma

Hi Varun

If you will use multiple groupby then it filters out more specifically.

Community Alums
Not applicable

Hi @Varun Sharma ,

groupBy(String name)

Provides the name of a field to use in grouping the aggregates.

May be called numerous times to set multiple group fields.

Parameters
Name Type Description
name String Name of the field.
Returns
Type Description
void Method does not return a value

Scoped equivalent:

To use the groupBy() method in a scoped application, use the corresponding scoped method: Scoped GlideAggregate - groupBy(String name).

Example:

var count = new GlideAggregate('incident');
count.addAggregate('MIN', 'sys_mod_count');
count.addAggregate('MAX', 'sys_mod_count');
count.addAggregate('AVG', 'sys_mod_count');
count.groupBy('category');
count.query();   
while (count.next()) {  
  var min = count.getAggregate('MIN', 'sys_mod_count');
  var max = count.getAggregate('MAX', 'sys_mod_count');
  var avg = count.getAggregate('AVG', 'sys_mod_count');
  var category = count.category.getDisplayValue();
  gs.log(category + " Update counts: MIN = " + min + " MAX = " + max + " AVG = " + avg);
}
Mark my answer correct & Helpful, if Applicable.

Thanks,

Sandeep

Hi Sandeep, 

 

here in code snippet you'll see the groupBy is used just once. 

in my code it's used thrice, i'm trying to understand how multiple groupby line by line impact the result.

 

Regards, 


Varun Sharma