メインコンテンツまでスキップ
バージョン: 26.x

COUNT

説明

指定された列のNULL以外のレコード数、または総レコード数を返します。

構文

COUNT(DISTINCT <expr> [,<expr>,...])
COUNT(*)
COUNT(<expr>)

パラメータ

Parameterデスクリプション
<expr>式が指定された場合、NULL以外のレコード数をカウントします。指定されない場合、行の総数をカウントします。

Return Value

戻り値の型はBigintです。exprがNULLの場合、カウントされません。

Example

-- setup
create table test_count(
id int,
name varchar(20),
sex int
) distributed by hash(id) buckets 1
properties ("replication_num"="1");

insert into test_count values
(1, '1', 1),
(2, '2', 1),
(3, '3', 1),
(4, '0', 1),
(4, '4', 1),
(5, NULL, 1);

create table test_insert(
id int,
name varchar(20),
sex int
) distributed by hash(id) buckets 1
properties ("replication_num"="1");

insert into test_insert values
(1, '1', 1),
(2, '2', 1),
(3, '3', 1),
(4, '0', 1),
(4, '4', 1),
(5, NULL, 1);
select count(*) from test_count;
+----------+
| count(*) |
+----------+
| 6 |
+----------+
select count(name) from test_insert;
+-------------+
| count(name) |
+-------------+
| 5 |
+-------------+
select count(distinct sex) from test_insert;
+---------------------+
| count(DISTINCT sex) |
+---------------------+
| 1 |
+---------------------+
select count(distinct id,sex) from test_insert;
+-------------------------+
| count(DISTINCT id, sex) |
+-------------------------+
| 5 |
+-------------------------+