toscalar()
適用対象: ✅Microsoft Fabric✅Azure データ エクスプローラー✅Azure Monitor✅Microsoft Sentinel
評価された式のスカラー定数値を返します。
この関数は、クエリの計算を段階分けする必要がある場合に役立ちます。 たとえば、イベントの総数を計算してから、その結果を使用して全イベントの一定割合を超えるグループをフィルター処理する場合などです。
2 つのステートメントはセミコロンで区切られます。
構文
toscalar(
式 (expression))
構文規則について詳しく知る。
パラメーター
件名 | タイプ | Required | 内容 |
---|---|---|---|
式 (expression) | string |
✔️ | スカラー値に変換する値。 |
返品
評価対象の式のスカラー定数値。 結果が表形式の場合、変換のために最初の列と最初の行が取得されます。
制限事項
toscalar()
は、各行に関数を適用するシナリオでは適用できません。 これは、クエリの実行中に関数を一定回数だけ計算できるためです。
通常、この制限に達すると、次のエラーが返されます: can't use '<column name>' as it is defined outside its row-context scope.
次の例では、クエリはエラーで失敗します。
'toscalar': can't use 'x' as it is defined outside its row-context scope.
let _dataset1 = datatable(x:long)[1,2,3,4,5];
let _dataset2 = datatable(x:long, y:long) [ 1, 2, 3, 4, 5, 6];
let tg = (x_: long)
{
toscalar(_dataset2| where x == x_ | project y);
};
_dataset1
| extend y = tg(x)
このエラーは、次の例のように、 join
演算子を使用して軽減できます。
let _dataset1 = datatable(x: long)[1, 2, 3, 4, 5];
let _dataset2 = datatable(x: long, y: long) [1, 2, 3, 4, 5, 6];
_dataset1
| join (_dataset2) on x
| project x, y
出力
x | y |
---|---|
1 | 2 |
3 | 4 |
5 | 6 |
例
このセクションの例では、構文を使用して作業を開始する方法を示します。
評価の範囲を設定する
Start
、End
、Step
をスカラー定数として評価してから、結果を使用して range
の評価を行います。
let Start = toscalar(print x=1);
let End = toscalar(range x from 1 to 9 step 1 | count);
let Step = toscalar(2);
range z from Start to End step Step | extend start=Start, end=End, step=Step
出力
z | start | end | ステップ |
---|---|---|---|
1 | 1 | 9 | 2 |
3 | 1 | 9 | 2 |
5 | 1 | 9 | 2 |
7 | 1 | 9 | 2 |
9 | 1 | 9 | 2 |
固定および動的 GUID を生成する
次の例では、toscalar()
を使用して、固定 guid
を生成し、正確に 1 回だけ計算し、guid
の動的な値を生成する方法を示します。
let g1 = toscalar(new_guid());
let g2 = new_guid();
range x from 1 to 2 step 1
| extend x=g1, y=g2
出力
x | y |
---|---|
e6a15e72-756d-4c93-93d3-fe85c18d19a3 | c2937642-0d30-4b98-a157-a6706e217620 |
e6a15e72-756d-4c93-93d3-fe85c18d19a3 | c6a48cb3-9f98-4670-bf5b-589d0e0dcaf5 |
関連コンテンツ
- スカラー関数型 一目で