aggregate
数値フィールドに対し、count、sum、average、keys、startおよびendなどの単純なデータ集計操作を実行します。aggregateコマンドはストア内のキーまたは行のマッチングを繰り返すため、指定されたキーまたは行のサイズによっては、完了するのにとても長い時間がかかる可能性があります。
aggregate tableは、aggregateのサブコマンドです。
aggregate table
aggregate table -name <name>
[-count] [-sum <field[,field,..]>]
[-avg <field[,field,..]>]
[-index <name>]
[-field <name> -value <value>]*
[-field <name> [-start <value>] [-end <value>]]
[-json <string>]
表の数値フィールドに対し、単純なデータ集計操作を実行します。
説明:
-
-name
操作を行う表を指定します。
-
-count
一致するレコードの件数を返します。
-
-sum
一致するフィールドの値の合計を返します。
-
-avg
一致するフィールドの値の平均を返します。
-
-index
使用する索引の名前を指定します。索引を使用する場合、指定されたフィールドは指定の索引に属する必要があり、索引エントリが一致する行全体で集計が実行されます。
-
-field
および-value
のペアを使用して、集計用の照合に使用する主キーのフィールド値を指定するか、キーを空にして表全体を照合できます。 -
-field
フラグと、その-start
および-end
フラグは、行の照合に使用する範囲の制限に使用できます。 -
-json
集計に使用するフィールドおよび値をJSON入力文字列として指定します。
次の例を参照してください。
# Create a table 'user_test' with an index on user_test(age):
kv-> execute 'CREATE TABLE user_test (id INTEGER,
firstName STRING, lastName STRING, age INTEGER, PRIMARY KEY (id))'
Statement completed successfully
kv-> execute 'CREATE INDEX idx1 on user_test (age)'
Statement completed successfully
# Insert 3 rows:
kv-> put table -name user_test -json
'{"id":1,"firstName":"joe","lastName":"wang","age":21}'
Operation successful, row inserted.
kv-> put table -name user_test -json
'{"id":2,"firstName":"jack","lastName":"zhao","age":32}'
Operation successful, row inserted.
kv-> put table -name user_test -json
'{"id":3,"firstName":"john","lastName":"gu","age":43}'
Operation successful, row inserted.
# Get count(*), sum(age) and avg(age) of rows in table:
kv-> aggregate table -name user_test -count -sum age -avg age
Row count: 3
Sum:
age(3 values): 96
Average:
age(3 values): 32.00
# Get count(*), sum(age) and avg(age) of rows where
age >= 30, idx1 is utilized to filter the rows:
kv-> aggregate table -name user_test -count -sum age
-avg age -index idx1 -field age -start 30
Row count: 2
Sum:
age(2 values): 75
Average:
age(2 values): 37.50