count

Syntax

count(X)

Details

size returns the number of elements in a vector or matrix, while count returns the number of non-null elements in a vector/matrix. count can be used in a SQL query, but size cannot. For tables, size and count both return the number of rows.

Parameters

X is a scalar/vector/matrix/table.

Returns

A scalar of type INT.

Examples

count(3 NULL 5 6);
// output: 3
size(3 NULL 5 6);
// output: 4

m=1 2 3 NULL 4 5$2:3;
m;
#0 #1 #2
1 3 4
2 5
count(m);
// output: 5
size(m);
// output: 6

t = table(1 NULL 3 as id, 3 NULL 9 as qty);
t;
id qty
1 3
3 9
count(t);
// output: 3
size(t);
// output: 3

If you need to check the number of rows in a DFS table, you must use count inside a SQL query. If you call count directly on a DFS table handle, it will always return 0.

Correct examples:

// Example 1
t=loadTable("dfs://csmar_l2", `SSEL2_BOND_ORDER)
select count(*) from t // Returns a table containing the row count of SSEL2_BOND_ORDER
// Example 2
t=loadTable("dfs://csmar_l2", `SSEL2_BOND_ORDER)
t1=select * from t
t1.count() // Returns an INT value indicating the row count of SSEL2_BOND_ORDER

Incorrect example:

t = loadTable("dfs://csmar_l2", `SSEL2_BOND_ORDER)
t.count()   // Returns 0