GlideAggregate - グローバル

  • リリースバージョン: Washingtondc
  • 更新日 2024年02月01日
  • 読む37読むのに数分
  • GlideAggregate を使用すると、データベース集計クエリを作成できます。

    - GlideAggregate クラスは GlideRecord の拡張であり、データベース集計 (AVG、COUNT、GROUP_CONCAT、GROUP_CONCAT_DISTINCT、MAX、MIN、STDDEV、SUM) クエリを提供します。この機能は、カスタマイズされたレポートを作成するとき、または計算フィールドの計算に役立ちます。

    GlideAggregate を通貨または価格フィールドで使用する際は、参照通貨値で作業しています。表示するには、集計値をユーザーのセッション通貨に変換してください。通貨または価格の値 (表示値) とその参照 通貨 値 (集計値) の間の換算レートは変化する可能性があるため、結果はユーザーが期待するものではない可能性があります。

    注:
    オンプレミス システムを使用する場合、このクラスが正しく機能するには、データベース サーバーのタイムゾーンを GMT / UTC に設定する必要があります。

    GlideAggregate - addAggregate(String agg、String name)

    データベースクエリに集計を追加します。

    表 : 1. パラメーター
    名前 タイプ 説明
    agg 文字列 使用する集計の名前。
    有効な値:
    • AVG:式の平均値。
    • COUNT:null 以外の値の数。
    • GROUP_CONCAT:グループの null 以外のすべての値を昇順で連結し、カンマ (,) で結合して、結果を文字列として返します。
    • GROUP_CONCAT_DISTINCT:グループの null 以外のすべての値を昇順で連結し、重複を削除して、カンマ (,) で結合し、結果を文字列として返します。
    • MAX:最大値。
    • MIN:最小値。
    • STDDEV:入力の標準偏差。
    • SUM:すべての値の合計。
    name 文字列 オプション。集計の結果をグループ化するフィールドの名前。

    デフォルト:Null

    表 : 2. 返される内容
    タイプ 説明
    なし

    次の例は、インシデント [incident] テーブルで GlideAggregate 関数を使用する方法を示しています。

    var incidentGA = new GlideAggregate('incident');
    incidentGA.groupBy('category');
    incidentGA.orderByAggregate('COUNT', 'sys_mod_count');
    
    incidentGA.addAggregate('AVG', 'sys_mod_count');
    incidentGA.addAggregate('COUNT', 'sys_mod_count');
    incidentGA.addAggregate('GROUP_CONCAT', 'sys_mod_count');
    incidentGA.addAggregate('GROUP_CONCAT_DISTINCT', 'sys_mod_count');
    incidentGA.addAggregate('MAX', 'sys_mod_count');
    incidentGA.addAggregate('MIN', 'sys_mod_count');
    incidentGA.addAggregate('STDDEV', 'sys_mod_count');
    incidentGA.addAggregate('SUM', 'sys_mod_count');
    
    incidentGA.query();
    
    while (incidentGA.next()) {
    gs.info('CATEGORY: ' + incidentGA.getValue('category'));
    gs.info('AVG: ' + incidentGA.getAggregate('AVG', 'sys_mod_count'));
    gs.info('COUNT: ' + incidentGA.getAggregate('COUNT', 'sys_mod_count'));
    gs.info('GROUP_CONCAT: ' + incidentGA.getAggregate('GROUP_CONCAT', 'sys_mod_count'));
    gs.info('GROUP_CONCAT_DISTINCT: ' + incidentGA.getAggregate('GROUP_CONCAT_DISTINCT', 'sys_mod_count'));
    gs.info('MAX: ' + incidentGA.getAggregate('MAX', 'sys_mod_count'));
    gs.info('MIN: ' + incidentGA.getAggregate('MIN', 'sys_mod_count'));
    gs.info('STDDEV: ' + incidentGA.getAggregate('STDDEV', 'sys_mod_count'));
    gs.info('SUM: ' + incidentGA.getAggregate('SUM', 'sys_mod_count'));
    gs.info(' ');
    }

    出力。

    CATEGORY: inquiry
    AVG: 8.42424242424242424242424242424242424242E00
    COUNT: 33
    GROUP_CONCAT: 0,0,0,0,1,2,2,4,4,4,5,5,5,6,6,6,6,6,6,6,6,7,8,8,8,8,9,15,15,19,32,33,36
    GROUP_CONCAT_DISTINCT: 0,1,2,4,5,6,7,8,9,15,19,32,33,36
    MAX: 36
    MIN: 0
    STDDEV: 9.15843294125113629932710147135171494439E00
    SUM: 278
      
    CATEGORY: software
    AVG: 21
    COUNT: 13
    GROUP_CONCAT: 3,4,5,5,6,7,9,10,10,11,14,94,95
    GROUP_CONCAT_DISTINCT: 3,4,5,6,7,9,10,11,14,94,95
    MAX: 95
    MIN: 3
    STDDEV: 3.27693962918655755532970326989852794087E01
    SUM: 273
      
    CATEGORY: Hardware
    AVG: 6.8
    COUNT: 10
    GROUP_CONCAT: 2,5,5,6,6,6,7,7,9,15
    GROUP_CONCAT_DISTINCT: 2,5,6,7,9,15
    MAX: 15
    MIN: 2
    STDDEV: 3.39280283999985933622820108982884699755E00
    SUM: 68
      
    CATEGORY: network
    AVG: 18
    COUNT: 5
    GROUP_CONCAT: 3,12,17,21,37
    GROUP_CONCAT_DISTINCT: 3,12,17,21,37
    MAX: 37
    MIN: 3
    STDDEV: 1.25698050899765347157025586536136512302E01
    SUM: 90
      
    CATEGORY: 
    AVG: 9.5
    COUNT: 4
    GROUP_CONCAT: 8,8,10,12
    GROUP_CONCAT_DISTINCT: 8,10,12
    MAX: 12
    MIN: 8
    STDDEV: 1.91485421551267621995020382273964310607E00
    SUM: 38
      
    CATEGORY: database
    AVG: 29
    COUNT: 2
    GROUP_CONCAT: 8,50
    GROUP_CONCAT_DISTINCT: 8,50
    MAX: 50
    MIN: 8
    STDDEV: 2.969848480983499602483546320840365965E01
    SUM: 58

    スコープ対応

    スコープ対象のアプリケーションで addAggregate() メソッドを使用するには、対応するスコープ対象のメソッド addAggregate()addAggregate()https://developer.servicenow.com/go_to_api.do?ID=r_ScopedGlideAggregateAddAggregate_String_String&v=vancouver を使用します。

    GlideAggregate - addBizCalendarTrend(文字列 fieldName, 文字列 bizCalendarSysId)

    ビジネスカレンダーによる傾向を集計クエリに追加します。このメソッドを使用すると、対応する GlideRecord の日時フィールドを選択し、指定されたビジネスカレンダーのタイムスパンに基づいてレコードをグループ化できます。

    注:
    このメソッドは、SQL クエリで JOIN 演算子を使用しません。
    表 : 3. パラメーター
    名前 タイプ 説明
    fieldName 文字列 レコードが含まれるグループまたはカレンダーのタイムスパンを決定するために使用する、関連する GlideRecord の日付/時刻フィールド。
    bizCalendarSysID 文字列 使用するカレンダー レコードSys_id。これは、目的の期間を含むカレンダーです。
    表 : 4. 返される内容
    タイプ 説明
    なし

    次のコード例は、ビジネスカレンダーの「月」の期間でグループ化されたインシデントレコードの数を示しています。

    var monthCal = "4d7ddda353f3001076bcddeeff7b12b1"
    var ga = new GlideAggregate('incident');
    ga.addAggregate('COUNT');
    ga.addBizCalendarTrend('opened_at', monthCal);
    ga.setGroup(false);
    ga.query();
    gs.print(ga.getRowCount());
    while (ga.next()) {
      gs.info(ga.getValue('bizcalref') + ', ' + ga.getValue('bizcalrefend') + ', ' + ga.getAggregate('COUNT'));
    }

    出力:

    13
    2015-08-01 00:00:00, 2015-09-01 00:00:00, 2
    2015-11-01 00:00:00, 2015-12-01 00:00:00, 2
    2016-08-01 00:00:00, 2016-09-01 00:00:00, 3
    2016-12-01 00:00:00, 2017-01-01 00:00:00, 1
    2018-08-01 00:00:00, 2018-09-01 00:00:00, 3
    2018-09-01 00:00:00, 2018-10-01 00:00:00, 3
    2018-10-01 00:00:00, 2018-11-01 00:00:00, 2
    2019-07-01 00:00:00, 2019-08-01 00:00:00, 2
    2020-06-01 00:00:00, 2020-07-01 00:00:00, 1
    2021-01-01 00:00:00, 2021-02-01 00:00:00, 1
    2023-04-01 00:00:00, 2023-05-01 00:00:00, 15
    2023-05-01 00:00:00, 2023-06-01 00:00:00, 23
    2023-07-01 00:00:00, 2023-08-01 00:00:00, 9

    GlideAggregate - addEncodedQuery(文字列 query)

    この集計に設定されている可能性のある他のクエリーにエンコードされたクエリーを追加します。

    表 : 5. パラメーター
    名前 タイプ 説明
    クエリ 文字列 集計に追加するエンコードされたクエリー文字列です。
    表 : 6. 返される内容
    タイプ 説明
    なし
    var agg = new GlideAggregate('incident');
    agg.addAggregate('count','category'); 
    agg.orderByAggregate('count', 'category'); 
    agg.orderBy('category'); 
    agg.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(2)'); 
    agg.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(2)'); 
    agg.query(); 
    while (agg.next()) { 
      var category = agg.category;
      var count = agg.getAggregate('count','category');
      var query = agg.getQuery();  
      var agg2 = new GlideAggregate('incident');   
      agg2.addAggregate('count','category');
      agg2.orderByAggregate('count', 'category');
      agg2.orderBy('category');
      agg2.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(3)');
      agg2.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(3)');
      agg2.addEncodedQuery(query);
      agg2.query();
      var last = "";
      while (agg2.next()) {
         last = agg2.getAggregate('count','category');      
      }
      gs.log(category + ": Last month:" + count + " Previous Month:" + last);
     
    }

    スコープ対応

    スコープ対象のアプリケーションで addEncodedQuery() メソッドを使用するには、対応するスコープ対象のメソッド addEncodedQuery()addEncodedQuery()https://developer.servicenow.com/go_to_api.do?ID=r_ScopedGlideAggregateAddEncodedQuery_String&v=vancouver を使用します。

    GlideAggregate - addHaving(文字列 name, 文字列 operator, 文字列 value)

    「having」要素を集計に追加します。例:select category, count(*) from incident group by category HAVING count(*) > 5

    表 : 7. パラメーター
    名前 タイプ 説明
    name 文字列 フィルターを適用する集計です。例:COUNT
    演算子 文字列 演算子記号です。例:<、>、=、!=
    value 文字列 クエリーを適用する値です。例:'5'
    表 : 8. 返される内容
    タイプ 説明
    なし
    var trend = new GlideAggregate('incident');
    trend.addTrend ('opened_at','Month');
    trend.addAggregate('COUNT');
    
    //addHaving limits the results returned to those in which the aggregate COUNT is greater than 2
    trend.addHaving('COUNT', '>', '2');
    trend.setGroup(false);
    trend.query();
    while(trend.next()) {
      gs.print(('Incidents by month ' + trend.getValue('timeref') + ' where count is more than 2 count is: ' + trend.getAggregate('COUNT'));
    }

    出力:

    Incidents by month 9/2018 where count is more than 2 count is: 3
    Incidents by month 10/2018 where count is more than 2 count is: 8
    Incidents by month 11/2018 where count is more than 2 count is: 14

    GlideAggregate - addTrend(文字列 fieldName, 文字列 timeInterval, 数値 numUnits)

    フィールドのトレンドを追加します。傾向を使用して、一定期間のパターンを表示します。

    注:
    結果を年別にグループ化 dayofweek するかどうかを制御するには、 GlideAggregate - setIntervalYearIncluded(ブーリアン b)を使用します。
    表 : 9. パラメーター
    名前 タイプ 説明
    fieldName 文字列 傾向が発生するフィールドの名前です。
    timeInterval 文字列 傾向の時間間隔です。
    有効な値:
    • 日付
    • dayofweek
    • 時間
    • 四半期
    • value
    numUnits 番号 オプション。timeIntervalminute の場合にのみ有効です。傾向に含める時間 (分) です。

    デフォルト値:1

    表 : 10. 返される内容
    タイプ 説明
    なし
    var trend = new GlideAggregate('incident');  
    trend.addTrend ('opened_at','month');  
    trend.addAggregate('COUNT');  
    trend.setGroup(false);  
    trend.query();  
    while(trend.next()) {  
       gs.print(trend.getValue('timeref') + ': ' + trend.getAggregate('COUNT'));  
    }  

    出力:

    9/2018: 3
    10/2018: 8
    11/2018: 14

    スコープ対応

    スコープ対象のアプリケーションで addTrend() メソッドを使用するには、対応するスコープ対象のメソッド addTrend()addTrend()https://developer.servicenow.com/go_to_api.do?ID=r_ScopedGlideAggAddTrend_S_S&v=vancouver を使用します。

    GlideAggregate - getAggregate(String agg、String name)

    現在のレコードからの集計の値を取得します。

    表 : 11. パラメーター
    名前 タイプ 説明
    agg 文字列 集計のタイプです。
    有効な値:
    • AVG:式の平均値。
    • COUNT:null 以外の値の数。
    • GROUP_CONCAT:グループの null 以外のすべての値を昇順で連結し、カンマ (,) で結合して、結果を文字列として返します。
    • GROUP_CONCAT_DISTINCT:グループの null 以外のすべての値を昇順で連結し、重複を削除して、カンマ (,) で結合し、結果を文字列として返します。
    • MAX:最大値。
    • MIN:最小値。
    • STDDEV:入力の標準偏差。
    • SUM:すべての値の合計。
    name 文字列 集計を取得するフィールドの名前
    表 : 12. 返される内容
    タイプ 説明
    文字列 集計の値。
    集計される値が FX 通貨値である場合、戻り値は <currency_code;currency_value> 形式になります。例:USD;134.980000
    注:
    指定されたフィールドに通貨タイプが混在する FX 通貨値が含まれている場合、このメソッドは値を集計できず、セミコロン (;) を返します。

    この例は、COUNT 集計を取得する方法を示しています。

    function doMyBusinessRule(assigned_to, number) {
      var agg = new GlideAggregate('incident');
      agg.addQuery('assigned_to', assigned_to);
      agg.addQuery('category', number);
      agg.addAggregate("COUNT");
      agg.query();
      var answer = 'false';
      if (agg.next()) {
        answer = agg.getAggregate("COUNT");
        if (answer > 0)
          answer = 'true';
        else
          answer = 'false';
      }
      return answer; 
    }

    この例は、FX 通貨フィールドの集計を示しています。

    var ga = new GlideAggregate('laptop_tracker');
    ga.addAggregate('SUM', 'cost');
    ga.groupBy('name');
    ga.query();
    while (ga.next()) {
      gs.info('Aggregate results ' + ga.getValue('name') + ' => ' + ga.getAggregate('SUM', 'cost'));
    }

    出力:

    *** Script: Aggregate results Apple MacBook Air => USD;1651.784280000000
    *** Script: Aggregate results Apple MacBook Pro => USD;1651.784280000000
    *** Script: Aggregate results Dell XPS => USD;470.852672000000
    *** Script: Aggregate results LG => 
    *** Script: Aggregate results Samsung Galaxy => USD;225.320000000000
    *** Script: Aggregate results Surface3 => USD;2895.560369520000
    *** Script: Aggregate results Toshiba => USD;9385.202875800000

    スコープ対応

    スコープ対象のアプリケーションで getAggregate() メソッドを使用するには、対応するスコープ対象のメソッド getAggregate()getAggregate() を使用します。

    GlideAggregate - getQuery()

    現在の集計を返すために必要なクエリーを取得します。

    表 : 13. パラメーター
    名前 タイプ 説明
    なし
    表 : 14. 返される内容
    タイプ 説明
    文字列 クエリー
    var agg = new GlideAggregate('incident');
    agg.addAggregate('count','category'); 
    agg.orderByAggregate('count', 'category'); 
    agg.orderBy('category'); 
    agg.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(2)'); 
    agg.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(2)'); 
    agg.query(); 
    while (agg.next()) { 
      var category = agg.category;
      var count = agg.getAggregate('count','category');
      var query = agg.getQuery();  
      var agg2 = new GlideAggregate('incident');   
      agg2.addAggregate('count','category');
      agg2.orderByAggregate('count', 'category');
      agg2.orderBy('category');
      agg2.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(3)');
      agg2.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(3)');
      agg2.addEncodedQuery(query);
      agg2.query();
      var last = "";
      while (agg2.next()) {
         last = agg2.getAggregate('count','category');      
      }
      gs.log(category + ": Last month:" + count + " Previous Month:" + last);
     
    }

    GlideAggregate - getRowCount()

    GlideAggregate オブジェクトの行数を取得します。

    表 : 15. パラメーター
    名前 タイプ 説明
    none
    表 : 16. 返される内容
    タイプ 説明
    番号 GlideAggregate オブジェクトの行数です。
    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();
      gs.info(count.getRowCount());
      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.info(category + " Update counts: MIN = " + min + " MAX = " + max + " AVG = " + avg);
      }
    出力:
    6
    Database Update counts: MIN = 8 MAX = 48 AVG = 28.0000
    Hardware Update counts: MIN = 4 MAX = 14 AVG = 6.6250
    Inquiry / Help Update counts: MIN = 0 MAX = 34 AVG = 6.5714
    Network Update counts: MIN = 3 MAX = 37 AVG = 18.6000
    Request Update counts: MIN = 5 MAX = 39 AVG = 13.4000
    Software Update counts: MIN = 4 MAX = 98 AVG = 24.0000

    スコープ対応

    スコープ対象のアプリケーションで getRowCount() メソッドを使用するには、対応するスコープ対象のメソッド Scoped GlideAggregate - getRowCount() を使用します。

    GlideAggregate - getTotal(文字列 agg, 文字列 name)

    集計を合計してレコード数を返します。

    表 : 17. パラメーター
    名前 タイプ 説明
    agg 文字列 使用する集計の名前。
    有効な値:
    • AVG:式の平均値。
    • COUNT:null 以外の値の数。
    • GROUP_CONCAT:グループの null 以外のすべての値を昇順で連結し、カンマ (,) で結合して、結果を文字列として返します。
    • GROUP_CONCAT_DISTINCT:グループの null 以外のすべての値を昇順で連結し、重複を削除して、カンマ (,) で結合し、結果を文字列として返します。
    • MAX:最大値。
    • MIN:最小値。
    • STDDEV:入力の標準偏差。
    • SUM:すべての値の合計。
    name 文字列 集計するフィールドの名前
    表 : 18. 返される内容
    タイプ 説明
    番号 レコードの数です。
    var incidentGA = new GlideAggregate('incident');
    incidentGA.addQuery('category', 'software'); 
    incidentGA.addAggregate('COUNT');  
    incidentGA.addTrend('opened_at','year'); // Counting number of incidents for software category per year
    incidentGA.setGroup(false);
    incidentGA.query();
    while(incidentGA.next()){
      gs.info('Incidents opened on year - '+incidentGA.getValue('timeref')+' - '+incidentGA.getAggregate('COUNT'));
    }
    gs.info('Total Aggregate Value >> '+ incidentGA.getTotal('COUNT')); 

    出力:

    Incidents opened on year - 2015 - 1
    Incidents opened on year - 2018 - 5
    Incidents opened on year - 2020 - 10
    Total Aggregate Value >> 16

    GlideAggregate getValue(String name)

    フィールドの値を返します。

    表 : 19. パラメーター
    名前 タイプ 説明
    name 文字列 フィールドの名前
    表 : 20. 返される内容
    タイプ 説明
    文字列 指定されたフィールドの値。無効な (結果セットの一部ではない) 場合は null を返します。
    var trend = new GlideAggregate('incident');
    trend.addTrend ('opened_at','Month');
    trend.addAggregate('COUNT');
    
    //addHaving limits the results returned to those in which the aggregate COUNT is greater than 2
    trend.addHaving('COUNT', '>', '2');
    trend.setGroup(false);
    trend.query();
    while(trend.next()) {
      gs.print(('Incidents by month ' + trend.getValue('timeref') + ' where count is more than 2 count is: ' + trend.getAggregate('COUNT'));
    }

    出力:

    Incidents by month 9/2018 where count is more than 2 count is: 3
    Incidents by month 10/2018 where count is more than 2 count is: 8
    Incidents by month 11/2018 where count is more than 2 count is: 14

    スコープ対応

    スコープ対象のアプリケーションで getValue() メソッドを使用するには、対応するスコープ対象のメソッド getValue()getValue() を使用します

    GlideAggregate - groupBy(文字列 name)

    集計のグループ化に使用するフィールドの名前を指定します。

    複数のグループフィールドを設定するために何度も呼び出されることがあります。

    表 : 21. パラメーター
    名前 タイプ 説明
    name 文字列 フィールドの名前
    表 : 22. 返される内容
    タイプ 説明
    なし
    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);
    }

    スコープ対応

    スコープ対象のアプリケーションで groupBy() メソッドを使用するには、対応するスコープ対象のメソッド groupBy()groupBy() を使用します。

    GlideAggregate orderBy(String name)

    指定したフィールドの値を使用して集計をオーダーします。フィールドはグループバイリストにも追加されます。

    表 : 23. パラメーター
    名前 タイプ 説明
    name 文字列 集計の注文に使用されるフィールドの名前

    または、 glidefunction:length(short_description) などの glidefunction を指定して集計を順序付けることもできます。glidefunctions の詳細については、「 glidefunction の操作」を参照してください。

    表 : 24. 返される内容
    タイプ 説明
    なし
    var agg = new GlideAggregate('incident');
    agg.addAggregate('count','category'); 
    agg.orderByAggregate('count', 'category'); 
    agg.orderBy('category'); 
    agg.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(2)'); 
    agg.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(2)'); 
    agg.query(); 
    while (agg.next()) { 
      var category = agg.category;
      var count = agg.getAggregate('count','category');
      var query = agg.getQuery();  
      var agg2 = new GlideAggregate('incident');   
      agg2.addAggregate('count','category');
      agg2.orderByAggregate('count', 'category');
      agg2.orderBy('category');
      agg2.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(3)');
      agg2.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(3)');
      agg2.addEncodedQuery(query);
      agg2.query();
      var last = "";
      while (agg2.next()) {
         last = agg2.getAggregate('count','category');      
      }
      gs.log(category + ": Last month:" + count + " Previous Month:" + last);
    }

    スコープ対応

    スコープ対象のアプリケーションで orderBy() メソッドを使用するには、対応するスコープ対象のメソッド orderBy()orderBy() を使用します。

    GlideAggregate - orderByAggregate(String agg、String name)

    指定した集計およびフィールドに基づいて集計をオーダーします。

    表 : 25. パラメーター
    名前 タイプ 説明
    agg 文字列 集計のタイプ
    有効な値:
    • AVG:式の平均値。
    • COUNT:null 以外の値の数。
    • GROUP_CONCAT:グループの null 以外のすべての値を昇順で連結し、カンマ (,) で結合して、結果を文字列として返します。
    • GROUP_CONCAT_DISTINCT:グループの null 以外のすべての値を昇順で連結し、重複を削除して、カンマ (,) で結合し、結果を文字列として返します。
    • MAX:最大値。
    • MIN:最小値。
    • STDDEV:入力の標準偏差。
    • SUM:すべての値の合計。
    fieldName 文字列 集計するフィールドの名前
    表 : 26. 返される内容
    タイプ 説明
    なし
    var agg = new GlideAggregate('incident');
    agg.addAggregate('count','category'); 
    agg.orderByAggregate('count', 'category'); 
    agg.orderBy('category'); 
    agg.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(2)'); 
    agg.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(2)'); 
    agg.query(); 
    while (agg.next()) { 
      var category = agg.category;
      var count = agg.getAggregate('count','category');
      var query = agg.getQuery();  
      var agg2 = new GlideAggregate('incident');   
      agg2.addAggregate('count','category');
      agg2.orderByAggregate('count', 'category');
      agg2.orderBy('category');
      agg2.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(3)');
      agg2.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(3)');
      agg2.addEncodedQuery(query);
      agg2.query();
      var last = "";
      while (agg2.next()) {
         last = agg2.getAggregate('count','category');      
      }
      gs.log(category + ": Last month:" + count + " Previous Month:" + last);
     
    }

    スコープ対応

    スコープ対象のアプリケーションで orderByAggregate() メソッドを使用するには、対応するスコープ対象のメソッド orderByAggregate()orderByAggregate() を使用します。

    GlideAggregate - query()

    クエリーを発行し、結果を取得します。

    表 : 27. パラメーター
    名前 タイプ 説明
    なし
    表 : 28. 返される内容
    タイプ 説明
    なし
    var agg = new GlideAggregate('incident');
    agg.addAggregate('count','category'); 
    agg.orderByAggregate('count', 'category'); 
    agg.orderBy('category'); 
    agg.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(2)'); 
    agg.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(2)'); 
    agg.query(); 
    while (agg.next()) { 
      var category = agg.category;
      var count = agg.getAggregate('count','category');
      var query = agg.getQuery();  
      var agg2 = new GlideAggregate('incident');   
      agg2.addAggregate('count','category');
      agg2.orderByAggregate('count', 'category');
      agg2.orderBy('category');
      agg2.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(3)');
      agg2.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(3)');
      agg2.addEncodedQuery(query);
      agg2.query();
      var last = "";
      while (agg2.next()) {
         last = agg2.getAggregate('count','category');      
      }
      gs.log(category + ": Last month:" + count + " Previous Month:" + last);

    スコープ対応

    スコープ対象のアプリケーションで query() メソッドを使用するには、対応するスコープ対象のメソッド query()query()https://developer.servicenow.com/go_to_api.do?ID=r_ScopedGlideAggregateQuery&v=vancouver を使用します。

    GlideAggregate - setAggregateWindow(数値 firstRow, 数値 lastRow)

    集計クエリに含めるテーブルの行数を制限します。

    表 : 29. パラメーター
    名前 タイプ 説明
    firstRow 数値 集計クエリに含める最初の行のゼロベースのインデックス (包含)。
    lastRow 数値 集計クエリに含める最後の行のゼロベースのインデックス (排他的) 。
    表 : 30. 返される内容
    タイプ 説明
    なし

    インシデント [incident] テーブルの最初の 10 件のレコードの各カテゴリの数を出力します。

    var incidentGroup = new GlideAggregate('incident');
    incidentGroup.addAggregate('COUNT', 'category');
    incidentGroup.setAggregateWindow(0, 10);
    incidentGroup.query();
    while (incidentGroup.next()) {
       var incidentCount = incidentGroup.getAggregate('COUNT', 'category');
       gs.info('{0} count: {1}', [incidentGroup.getValue('category'), incidentCount]);
    }

    出力:

    database count: 1
    Hardware count: 1
    inquiry count: 7
    software count: 1

    スコープ対応

    スコープ対象のアプリケーションで setAggregateWindow() メソッドを使用するには、対応するスコープ対象のメソッド setAggregateWindow()setAggregateWindow() を使用します。

    GlideAggregate - setGroup(ブール b)

    結果をグループ化するかどうかを設定します。

    表 : 31. パラメーター
    名前 タイプ 説明
    b ブーリアン 結果をグループ化するかどうかを示すフラグです。
    有効な値:
    • true:結果をグループ化します。
    • false:結果をグループ化しません。
    表 : 32. 返される内容
    タイプ 説明
    なし
    var ga = new GlideAggregate('incident');
    ga.addAggregate('COUNT', 'category');
     
    ga.setGroup(true);

    スコープ対応

    スコープ対象のアプリケーションで setGroup() メソッドを使用するには、対応するスコープ対象のメソッド setGroup()setGroup()https://developer.servicenow.com/go_to_api.do?ID=r_ScopedGlideAggregateSetGroup_Boolean&v=vancouver を使用します。

    GlideAggregate - setIntervalYearIncluded(ブーリアン b)

    曜日の傾向の結果を年別にグループ化するかどうかを設定します。これらの傾向は、時間間隔を指定して dayofweekaddTrend() メソッドを使用して作成されます。

    依存関係:GlideAggregate - addTrend('&lt;fieldName>', 'dayofweek')。

    表 : 33. パラメーター
    名前 タイプ 説明
    b ブール 曜日の時間間隔を持つ傾向に年を含めるかどうかを示すフラグ。
    有効な値:
    • true:曜日の結果を年別にグループ化します。
    • false:時間間隔の結果から年を除外します。

    デフォルト:true

    表 : 34. 返される内容
    タイプ 説明
    なし

    次の例は、過去 6 か月間に作成されたインシデントをカウントする方法を示しています。インシデントは曜日で区切られますが、年は含まれません。たとえば、木曜日のデフォルトの結果には、 2023 年木曜日: 16 などの年が含まれます。

    var incidentGroup = new GlideAggregate('incident');
    incidentGroup.addEncodedQuery("sys_created_onRELATIVEGT@month@ago@6");
    incidentGroup.addTrend('sys_created_on', 'dayofweek');
    incidentGroup.addAggregate('COUNT');
    incidentGroup.setIntervalYearIncluded(false);
    incidentGroup.query();
    while (incidentGroup.next()) {
        gs.info(incidentGroup.getValue('timeref') + ': ' + incidentGroup.getAggregate('COUNT'))};

    出力:

    Sunday: 1
    Monday: 15
    Tuesday: 1
    Wednesday: 7
    Thursday: 16
    Saturday: 1

    スコープ対応

    スコープ対象のアプリケーションで setIntervalYearIncluded() メソッドを使用するには、対応するスコープ対象のメソッド setIntervalYearIncluded()setIntervalYearIncluded() を使用します。