GlideAggregate - スコープ対象
GlideAggregate API を使用すると、データベース集計クエリを作成できます。
GlideAggregate クラスは GlideRecord クラスの拡張であり、データベース集計 (AVG、COUNT、GROUP_CONCAT、GROUP_CONCAT_DISTINCT、MAX、MIN、STDDEV、SUM) クエリを提供します。この機能は、カスタマイズされたレポートを作成するとき、または計算フィールドの計算に役立ちます。
通貨フィールドまたは価格フィールドで GlideAggregate メソッドを使用すると、参照通貨値を使用して作業することになります。表示するには、集計値をユーザーのセッション通貨に変換してください。通貨または価格の値 (表示値) とその参照 通貨 値 (集計値) の間の換算レートは変化する可能性があるため、結果がユーザーの期待とは異なる場合があります。
ScopedGlideAggregate - GlideAggregate(String tableName)
指定されたテーブルに GlideAggregate オブジェクトを作成します。
| 名前 | タイプ | 説明 |
|---|---|---|
| tableName | 文字列 | テーブルの名前です。 |
var count = new GlideAggregate('incident');
ScopedGlideAggregate - addAggregate(String agg、String name)
データベースクエリに集計を追加します。
| 名前 | タイプ | 説明 |
|---|---|---|
| agg | 文字列 | 使用する集計の名前。 有効な値:
|
| name | 文字列 | オプション。集計の結果をグループ化するフィールドの名前。 デフォルト:Null |
| タイプ | 説明 |
|---|---|
| なし |
次の例は、インシデント [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
ScopedGlideAggregate - addEncodedQuery(String query)
この集計に設定されている可能性のある他のクエリーにエンコードされたクエリーを追加します。
| 名前 | タイプ | 説明 |
|---|---|---|
| クエリ | 文字列 | 集計に追加するエンコードされたクエリー |
| タイプ | 説明 |
|---|---|
| なし |
//Number of incidents varies depending on the current state
//of the incident table
var count = new GlideAggregate('incident');
count.addEncodedQuery('active=true');
count.addAggregate('COUNT');
count.query();
var incidents = 0;
if (count.next())
incidents = count.getAggregate('COUNT');
gs.info(incidents);
ScopedGlideAggregate - addQuery(String name, String operator, String value)
集計にクエリーを追加します。
| 名前 | タイプ | 説明 |
|---|---|---|
| name | 文字列 | 追加するクエリーです。 |
| 演算子 | 文字列 | クエリーの演算子です。 |
| value | 文字列 | クエリーに含める値のリストです。 |
| タイプ | 説明 |
|---|---|
| GlideQueryCondition | クエリー条件です。 |
//Number of incidents varies depending on the current state
//of the incident table
var count = new GlideAggregate('incident');
count.addQuery('active', '=','true');
count.addAggregate('COUNT', 'category');
count.query();
while (count.next()) {
var category = count.category;
var categoryCount = count.getAggregate('COUNT', 'category');
gs.info("There are currently " + categoryCount + " incidents with a category of " + category);
}
There are currently 1 incidents with a category of database
There are currently 5 incidents with a category of hardware
There are currently 42 incidents with a category of inquiry
There are currently 4 incidents with a category of network
There are currently 4 incidents with a category of request
There are currently 7 incidents with a category of softwareScopedGlideAggregate - addNotNullQuery(String fieldName)
集計に not null クエリーを追加します。
| 名前 | タイプ | 説明 |
|---|---|---|
| fieldname | 文字列 | フィールドの名前 |
| タイプ | 説明 |
|---|---|
| GlideQueryCondition | スコープ付きクエリー条件です。 |
var count = new GlideAggregate('incident');
count.addNotNullQuery('short_description');
count.query(); // Issue the query to the database to get all records
while (count.next()) {
// add code here to process the aggregate
}
ScopedGlideAggregate - addNullQuery(String fieldName)
集計にヌル クエリーを追加します。
| 名前 | タイプ | 説明 |
|---|---|---|
| fieldName | 文字列 | フィールドの名前 |
| タイプ | 説明 |
|---|---|
| GlideQueryCondition | Scoped クエリー条件 |
var count = new GlideAggregate('incident');
count.addNullQuery('short_description');
count.query(); // Issue the query to the database to get all records
while (count.next()) {
// add code here to process the aggregate
}
スコープ付き GlideAggregate - addTrend(文字列 fieldName, 文字列 timeInterval, 数値 numUnits)
指定されたフィールドの傾向を追加します。傾向を使用して、一定期間にわたるパターンを示します。
| 名前 | タイプ | 説明 |
|---|---|---|
| fieldName | 文字列 | 傾向が発生するフィールドの名前です。 |
| timeInterval | 文字列 | 傾向の時間間隔です。 有効な値:
|
| numUnits | 番号 | オプション。timeInterval が minute の場合にのみ有効です。傾向に含める時間 (分) です。 デフォルト値:1 |
| タイプ | 説明 |
|---|---|
| なし |
var ga = new GlideAggregate('incident');
ga.addAggregate('COUNT'); // Count all incidents opened each quarter
ga.addTrend('opened_at', 'quarter');
ga.query();
while(ga.next()) {
gs.info([ga.getValue('timeref'), ga.getAggregate('COUNT')]);
}
3/2018, 9
4/2018, 2
1/2019, 38
2/2019, 310ScopedGlideAggregate - getAggregate(String agg、String name)
現在のレコードからの集計の値を返します。
| 名前 | タイプ | 説明 |
|---|---|---|
| agg | 文字列 | 集計のタイプです。 有効な値:
|
| name | 文字列 | 集計を実行するフィールドの名前です。 |
| タイプ | 説明 |
|---|---|
| 文字列 | 集計の値です。 集計される値が FX 通貨値である場合、戻り値は <currency_code;currency_value> 形式になります。例:USD;134.980000 注: 指定されたフィールドに通貨タイプが混在する FX 通貨値が含まれている場合、このメソッドは値を集計できず、セミコロン (;) を返します。 |
インシデントテーブルのレコード数を返す集計を表示します。
var count = new GlideAggregate('incident');
count.addAggregate('COUNT');
count.query();
var incidents = 0;
if (count.next()) {
incidents = count.getAggregate('COUNT');
}
//Number of incidents varies depending on the current state
//of the incident table
gs.info('Number of incidents: ' + incidents);
出力:Number of incidents: 63
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
ScopedGlideAggregate - getAggregateEncodedQuery()
現在の集計を返すために必要なクエリーを取得します。
| 名前 | タイプ | 説明 |
|---|---|---|
| なし |
| タイプ | 説明 |
|---|---|
| 文字列 | 集計を取得するためのエンコードされたクエリーです。 |
var count = new GlideAggregate('incident');
count.addAggregate('MIN', 'sys_mod_count');
count.groupBy('category');
count.query();
while (count.next()) {
gs.info(count.getAggregateEncodedQuery());
}
category=database
category=hardware
category=inquiry
category=network
category=request
category=softwareScopedGlideAggregate - getEncodedQuery()
エンコードされたクエリーを取得します。
| 名前 | タイプ | 説明 |
|---|---|---|
| none |
| タイプ | 説明 |
|---|---|
| 文字列 | エンコードされたクエリーです。 |
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.getEncodedQuery());
ORDERBYcategory^GROUPBYcategoryScopedGlideAggregate - getRowCount()
GlideAggregate オブジェクトの行数を取得します。
| 名前 | タイプ | 説明 |
|---|---|---|
| none |
| タイプ | 説明 |
|---|---|
| 番号 | 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.0000ScopedGlideAggregate - getTableName()
この GlideAggregate オブジェクトに関連付けられたテーブル名を取得します。
| 名前 | タイプ | 説明 |
|---|---|---|
| none |
| タイプ | 説明 |
|---|---|
| 文字列 | テーブル名 |
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.getTableName());
ScopedGlideAggregate - getValue(String name)
指定されたフィールドの値を返します。
| 名前 | タイプ | 説明 |
|---|---|---|
| name | 文字列 | 返される現在のテーブル内のフィールドの名前です。 |
| タイプ | 説明 |
|---|---|
| 文字列 | 指定されたフィールドの値。無効な (結果セットの一部ではない) 場合は null を返します。 |
var count = new GlideAggregate('incident');
count.addAggregate('MAX', 'sys_mod_count');
count.groupBy('category');
count.query();
while (count.next()) {
gs.info(count.getValue('category') + " category had " + count.getAggregate('MAX', 'sys_mod_count') + " updates");
}
出力:
category had 12 updates
hardware category had 15 updates
inquiry category had 36 updates
network category had 37 updates
software category had 95 updates
ScopedGlideAggregate - groupBy(String name)
集計のグループ化に使用するフィールドの名前を指定します。
複数のグループフィールドを設定するために何度も呼び出されることがあります。
| 名前 | タイプ | 説明 |
|---|---|---|
| name | 文字列 | フィールドの名前 |
| タイプ | 説明 |
|---|---|
| なし |
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.info(category + " Update counts: MIN = " + min + " MAX = " + max + " AVG = " + avg);
}
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
ScopedGlideAggregate - hasNext()
GlideAggregate オブジェクトにそれ以上のレコードがあるかどうかを判定します。
| 名前 | タイプ | 説明 |
|---|---|---|
| none |
| タイプ | 説明 |
|---|---|
| ブーリアン | クエリーセットにさらに結果が含まれているかどうかを示すフラグです。 可能な値:
|
var agg = new GlideAggregate('incident');
agg.addAggregate('AVG', 'sys_mod_count');
agg.groupBy('category');
agg.query();
while (agg.hasNext()) {
agg.next();
var avg = agg.getAggregate('AVG', 'sys_mod_count');
var category = agg.category.getDisplayValue();
gs.info(category + ': AVG = ' + avg);
}
Database: AVG = 32.5000
Hardware: AVG = 12.0000
Inquiry / Help: AVG = 7.6667
Network: AVG = 24.0000
Request: AVG = 16.4000
Software: AVG = 27.0833ScopedGlideAggregate - next()
GlideAggregate の次のレコードに移動します。
| 名前 | タイプ | 説明 |
|---|---|---|
| none |
| タイプ | 説明 |
|---|---|
| ブーリアン | クエリーセットにさらに結果が含まれているかどうかを示すフラグです。 可能な値:
|
var count = new GlideAggregate('incident');
count.addAggregate('COUNT');
count.query();
var incidents = 0;
if (count.next()) {
incidents = count.getAggregate('COUNT');
gs.info(incidents);
}
ScopedGlideAggregate - orderBy(String name)
指定したフィールドの値を使用して集計をオーダーします。このフィールドは [グループ別] リストにも追加されます。
| 名前 | タイプ | 説明 |
|---|---|---|
| name | 文字列 | 集計を順序付けるフィールドの名前 または、 |
| タイプ | 説明 |
|---|---|
| なし |
var agg = new GlideAggregate('incident');
agg.addAggregate('count', 'category');
agg.orderBy('category');
agg.query();
while (agg.next()) {
var category = agg.category;
var count = agg.getAggregate('count', 'category');
var agg2 = new GlideAggregate('incident');
agg2.addAggregate('count', 'category');
agg2.orderBy('category');
gs.info(category + ": Current number of incidents:" + count);
}
database: Current number of incidents:2
hardware: Current number of incidents:8
inquiry: Current number of incidents:28
network: Current number of incidents:5
request: Current number of incidents:5
software: Current number of incidents:11ScopedGlideAggregate - orderByAggregate(String agg、String fieldName)
指定した集計およびフィールドに基づいて集計をオーダーします。
| 名前 | タイプ | 説明 |
|---|---|---|
| agg | 文字列 | 集計のタイプ |
| fieldName | 文字列 | 集計するフィールドの名前 |
| タイプ | 説明 |
|---|---|
| なし |
ga.addAggregate('COUNT', 'category');
ga.orderByAggregate('count', 'category');
ga.query();
while(ga.next()) {
gs.info('Category: ' + ga.category + ' ' + ga.getAggregate('COUNT', 'category'));
}
出力:
Category: inquiry 18
Category: software 11
Category: hardware 7
Category: network 5
Category: request 5
Category: 4
Category: database 2
ScopedGlideAggregate - orderByDesc(String name)
指定されたフィールドに基づいて降順で集計をソートします。フィールドはグループバイリストにも追加されます。
| 名前 | タイプ | 説明 |
|---|---|---|
| name | 文字列 | フィールドの名前 |
| タイプ | 説明 |
|---|---|
| なし |
var agg = new GlideAggregate('incident');
agg.addAggregate('count', 'category');
agg.orderByDesc('category');
agg.query();
while (agg.next()) {
var category = agg.category;
var count = agg.getAggregate('count', 'category');
var agg2 = new GlideAggregate('incident');
agg2.addAggregate('count', 'category');
agg2.orderBy('category');
gs.info(category + ": Current number of incidents:" + count);
}
software: Current number of incidents:11
request: Current number of incidents:5
network: Current number of incidents:5
inquiry: Current number of incidents:28
hardware: Current number of incidents:8
database: Current number of incidents:2
ScopedGlideAggregate - query()
クエリーを発行し、結果を取得します。
| 名前 | タイプ | 説明 |
|---|---|---|
| なし |
| タイプ | 説明 |
|---|---|
| なし |
var count = new GlideAggregate('incident');
count.addAggregate('COUNT');
count.query();
var incidents = 0;
if (count.next()) {
incidents = count.getAggregate('COUNT');
}
gs.info('Number of incidents: ' + incidents);
スコープ付き GlideAggregate - setAggregateWindow(数値 firstRow, 数値 lastRow)
集計クエリに含めるテーブルの行数を制限します。
| 名前 | タイプ | 説明 |
|---|---|---|
| firstRow | 数値 | 集計クエリに含める最初の行のゼロベースのインデックス (包含)。 |
| lastRow | 数値 | 集計クエリに含める最後の行のゼロベースのインデックス (排他的) 。 |
| タイプ | 説明 |
|---|---|
| なし |
インシデント [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
ScopedGlideAggregate - setGroup(Boolean b)
返される結果をグループ化するかどうかを設定します。
| 名前 | タイプ | 説明 |
|---|---|---|
| b | ブーリアン | 結果をグループ化するかどうかを示すフラグです。 有効な値:
|
| タイプ | 説明 |
|---|---|
| なし |
var ga = new GlideAggregate('incident');
ga.addAggregate('COUNT', 'category');
ga.setGroup(true);
ga.groupBy("category");
ga.query();
while(ga.next()) {
gs.info('Category ' + ga.category + ' ' + ga.getAggregate('COUNT', 'category'));
}
出力:
Category database 2
Category hardware 7
Category inquiry 18
Category network 5
Category request 5
Category software 11
スコープ対象 GlideAggregate - setIntervalYearIncluded(ブール値 b)
曜日の傾向について結果を年別にグループ化するかどうかを設定します。これらの傾向は、dayofweek時間間隔で addTrend() メソッドを使用して作成されます。
依存関係: スコープ付き GlideAggregate - addTrend('<fieldName>', 'dayofweek')。
| 名前 | タイプ | 説明 |
|---|---|---|
| b | ブール | 曜日の時間間隔で傾向に年を含めるかどうかを示すフラグ。 有効な値:
デフォルト:true |
| タイプ | 説明 |
|---|---|
| なし |
次の例は、過去 6 か月間に作成されたインシデントをカウントする方法を示しています。インシデントは曜日で区切られますが、年は含まれません。たとえば、木曜日のデフォルトの結果には、「 Thursday/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