I have already created a function that allows me to have a table obtained through groupby and aggregation function. Since the column I have grouped has so many values, I would like to sort descending the rows (by the "percentage" column, created by division of 2 columns) and then keep only the first 5 rows.
This I would like to do in TypeScript, perhaps by appending it under the groupby function, if possible.
Thank u!!!
Output example:
| column1 | percentage |
|---|---|
| 1 | 0.75 |
| 2 | 0.65 |
| 3 | 0.63 |
| 4 | 0.61 |
| 5 | 0.55 |
Code example:
export class funzione1 {
@Function()
public async test1(object1: ObjectSet<object1>):
Promise<TwoDimensionalAggregation<string>> {
const numerators = await object1.groupBy(e => e.column1.topValues())
.sum(e => e.column2);
const denominators = await object1.groupBy(e => e.column1.topValues())
.sum(e => e.column3);
return this.divideTwoDimensional(numerators, denominators);
}
private divideTwoDimensional(numerators:TwoDimensionalAggregation<string>,
denominators: TwoDimensionalAggregation<string>):
TwoDimensionalAggregation<string> {
const percentage = numerators.buckets.map((bucket, i) => {
const numerator = bucket.value;
const denominator = denominators.buckets[i].value;
if (denominator === 0) {
return { key: bucket.key, value: 0 };
}
return { key: bucket.key, value: numerator / denominator }
});
return { buckets: percentage };
}