{ “title”: “COUNT”, “language”: “en” }

Description

Returns the number of non-NULL records for the specified column, or the total number of records.

Syntax

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

Parameters

ParameterDescription
<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

-- 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 |
+-------------------------+