コンテンツにスキップ

「等高」バケット方式を使用して、データ分布のヒストグラムを生成します。

func.histogram(<expr>)

詳細は SQLの例 を参照してください。

HISTOGRAM(<expr>)
-- The following two forms are equivalent:
HISTOGRAM(<max_num_buckets>)(<expr>)
HISTOGRAM(<expr> [, <max_num_buckets>])
パラメーター 説明
expr expr のデータ型は並べ替え可能である必要があります。
max_num_buckets バケットの最大数を指定する、オプションの正の整数。デフォルトは 128 です。

空の文字列、または次の構造の JSON オブジェクトを返します。

  • buckets: 詳細情報を持つバケットのリスト:
    • lower: バケットの下限。
    • upper: バケットの上限。
    • count: バケット内の要素数。
    • pre_sum: 現在のバケットまでの要素の累積数。
    • ndv: バケット内の一意な値の数。

この例は、HISTOGRAM 関数が histagg テーブルの c_int 値の分布を分析し、バケットの境界、一意な値の数、要素数、累積数を返す様子を示しています。

CREATE TABLE histagg (
c_id INT,
c_tinyint TINYINT,
c_smallint SMALLINT,
c_int INT
);
INSERT INTO histagg VALUES
(1, 10, 20, 30),
(1, 11, 21, 33),
(1, 11, 12, 13),
(2, 21, 22, 23),
(2, 31, 32, 33),
(2, 10, 20, 30);
SELECT HISTOGRAM(c_int) FROM histagg;
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ histogram(c_int) │
├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ [{"lower":"13","upper":"13","ndv":1,"count":1,"pre_sum":0},{"lower":"23","upper":"23","ndv":1,"count":1,"pre_sum":1},{"lower":"30","upper":"30","ndv":1,"count":2,"pre_sum":2},{"lower":"33","upper":"33","ndv":1,"count":2,"pre_sum":4}] │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

結果は JSON 配列として返されます。

[
{
"lower": "13",
"upper": "13",
"ndv": 1,
"count": 1,
"pre_sum": 0
},
{
"lower": "23",
"upper": "23",
"ndv": 1,
"count": 1,
"pre_sum": 1
},
{
"lower": "30",
"upper": "30",
"ndv": 1,
"count": 2,
"pre_sum": 2
},
{
"lower": "33",
"upper": "33",
"ndv": 1,
"count": 2,
"pre_sum": 4
}
]

この例は、HISTOGRAM(2) が c_int の値を 2 つのバケットにグループ化する様子を示しています。

SELECT HISTOGRAM(2)(c_int) FROM histagg;
-- Or
SELECT HISTOGRAM(c_int, 2) FROM histagg;
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ histogram(2)(c_int) │
├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ [{"lower":"13","upper":"30","ndv":3,"count":4,"pre_sum":0},{"lower":"33","upper":"33","ndv":1,"count":2,"pre_sum":4}] │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

結果は JSON 配列として返されます。

[
{
"lower": "13",
"upper": "30",
"ndv": 3,
"count": 4,
"pre_sum": 0
},
{
"lower": "33",
"upper": "33",
"ndv": 1,
"count": 2,
"pre_sum": 4
}
]