bin()

値を切り捨てて、指定された bin サイズの倍数である整数にします。

多くの場合、summarize by ... と組み合わせて使用します。 値のセットが分散している場合に、特定の値ごとの小さなセットにグループ化されます。

bin()関数と floor() 関数は同等です

構文

bin(value,roundTo)

構文規則について詳しく知る。

パラメーター

名前 必須 説明
value int、long、real、 timespan、または datetime ✔️ 切り捨てする値。
roundTo int、long、real、または timespan ✔️ を分割する "bin size" です。

戻り値

value 未満で、roundTo の最も近い倍数。 Null 値、ビン サイズが null、またはビン サイズが負の場合、結果は null になります。

数値ビン

print bin(4.5, 1)

出力

print_0
4

Timespan bin

print bin(time(16d), 7d)

出力

print_0
14:00:00:00

Datetime bin

print bin(datetime(1970-05-11 13:45:07), 1d)

出力

print_0
1970-05-11T00:00:00Z

null ビンを使用してテーブルを埋める

テーブルに対応する行がないビンの行がある場合は、テーブルにそれらのビンを埋め込むことをおすすめします。次のクエリでは、カリフォルニア州の強風雨イベントを 4 月に 1 週間見ます。 ただし、一部の日にはイベントはありません。

let Start = datetime('2007-04-07');
let End = Start + 7d;
StormEvents
| where StartTime between (Start .. End)
| where State == "CALIFORNIA" and EventType == "Strong Wind"
| summarize PropertyDamage=sum(DamageProperty) by bin(StartTime, 1d)

出力

StartTime PropertyDamage
2007-04-08T00:00:00Z 3000
2007-04-11T00:00:00Z 1000
2007-04-12T00:00:00Z 105000

1 週間を表すために、次のクエリでは、不足している日の結果テーブルに null 値が埋め込まれます。 プロセスの詳細な説明を次に示します。

  1. 演算子を union 使用して、テーブルに行を追加します。
  2. range 演算子により、1 つの行と列を含むテーブルが生成されます。
  3. 関数に対する演算子は mv-expandrange と の間 StartTime にビンがあるのと同じ数の行を作成します EndTime
  4. PropertyDamage には 0 を使用します。
  5. 演算子は summarize 、元のテーブルから式によって union 生成されたテーブルに bin をグループ化します。 このプロセスにより、出力には、ビンごとに 0 個または元の数の値を持つ 1 つの行が含まれるようになります。
let Start = datetime('2007-04-07');
let End = Start + 7d;
StormEvents
| where StartTime between (Start .. End)
| where State == "CALIFORNIA" and EventType == "Strong Wind"
| union (
    range x from 1 to 1 step 1
    | mv-expand StartTime=range(Start, End, 1d) to typeof(datetime)
    | extend PropertyDamage=0
    )
| summarize PropertyDamage=sum(DamageProperty) by bin(StartTime, 1d)

出力

StartTime PropertyDamage
2007-04-07T00:00:00Z 0
2007-04-08T00:00:00Z 3000
2007-04-09T00:00:00Z 0
2007-04-10T00:00:00Z 0
2007-04-11T00:00:00Z 1000
2007-04-12T00:00:00Z 105000
2007-04-13T00:00:00Z 0
2007-04-14T00:00:00Z 0