How to get max value from source table to target table?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-13-2023 05:39 PM
Hello,
I have source table i.e. daily_cpu_table. As file name has 2 same records with different total. So in target table i have to show max value for that particular date, like in capture there are 2 dates(1st Oct, 2nd Oct) with total field is different for same file name. So in target table, it should fetch max value from total field for that particular date with file name. Can anyone help me with this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-13-2023 07:25 PM
Let's use GlideAggregate.
"The GlideAggregate class is an extension of GlideRecord and provides database aggregation (AVG, COUNT, GROUP_CONCAT, GROUP_CONCAT_DISTINCT, MAX, MIN, STDDEV, SUM) queries."
Sample script below I use to get the MAX Total Planned Cost in the Cost Plan [cost_plan] table with a specific date query.
var date_query = "sys_created_onON2022-04-08@javascript:gs.dateGenerate('2022-04-08','start')@javascript:gs.dateGenerate('2022-04-08','end')";
var gr = new GlideAggregate('cost_plan');
gr.addEncodedQuery(date_query);
gr.addAggregate('MAX', 'cost_local_currency');
gr.setGroup(false);
gr.query();
if(gr.next()){
gs.log(gr.getAggregate('MAX', 'cost_local_currency'));
}
Cheers,
Tai Vu