| --- |
| { |
| "title": "COUNT", |
| "language": "en" |
| } |
| --- |
| |
| ## Description |
| |
| Returns the number of non-NULL records for the specified column, or the total number of records. |
| |
| ## Syntax |
| |
| ```sql |
| COUNT(DISTINCT <expr> [,<expr>,...]) |
| COUNT(*) |
| COUNT(<expr>) |
| ``` |
| |
| ## Parameters |
| |
| | Parameter | Description | |
| | -- | -- | |
| | `<expr>` | If an expression is specified, counts the number of non-NULL records; otherwise, counts the total number of rows. | |
| |
| ## Return Value |
| |
| The return type is Bigint. If expr is NULL, it is not counted. |
| |
| ## Example |
| |
| ```sql |
| -- 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); |
| ``` |
| |
| ```sql |
| select count(*) from test_count; |
| ``` |
| |
| ```text |
| +----------+ |
| | count(*) | |
| +----------+ |
| | 6 | |
| +----------+ |
| ``` |
| |
| ```sql |
| select count(name) from test_insert; |
| ``` |
| |
| ```text |
| +-------------+ |
| | count(name) | |
| +-------------+ |
| | 5 | |
| +-------------+ |
| ``` |
| |
| ```sql |
| select count(distinct sex) from test_insert; |
| ``` |
| |
| ```text |
| +---------------------+ |
| | count(DISTINCT sex) | |
| +---------------------+ |
| | 1 | |
| +---------------------+ |
| ``` |
| |
| ```sql |
| select count(distinct id,sex) from test_insert; |
| ``` |
| |
| ```text |
| +-------------------------+ |
| | count(DISTINCT id, sex) | |
| +-------------------------+ |
| | 5 | |
| +-------------------------+ |
| ``` |