请注意:
下文中的一些示例引用自 ClickHouse 社区文档 并经过一定修改确保可以在 ByteHouse 中正常使用。
Calculates the absolute value of the number (a). That is, if a < 0, it returns -a. For unsigned types it does not do anything. For signed integer types, it returns an unsigned number.
Syntax
abs(x)
Arguments
x
– The number.Returned value
Example
SELECT abs(-2);
Result:
┌─abs(-2)─┐ │ 2 │ └─────────┘
Calculates the quotient of the numbers. The result type is always a floating-point type.
It is not integer division. For integer division, use the ‘intDiv’ function.
When dividing by zero you get ‘inf’, ‘-inf’, or ‘nan’.
Syntax
divide(a, b) # a / b operator
Arguments
a
– The number.b
– The number.Returned value
Example
SELECT divide(50, 2);
Result:
┌─divide(50, 2)─┐ │ 2.5e+01 │ └───────────────┘
Returns the greatest common divisor of the numbers.
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
Syntax
gcd(a, b)
Arguments
a
– The number.b
– The number.Returned value
Example
SELECT gcd(27,18);
Result:
┌─gcd(27, 18)─┐ │ 9 │ └─────────────┘
Calculates the quotient of the numbers. Divides into integers, rounding down (by the absolute value).
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
Syntax
intDiv(a, b)
Arguments
a
– The number.b
– The number.Returned value
Example
SELECT intDiv(10, 2);
Result:
┌─intDiv(10, 2)─┐ │ 5 │ └───────────────┘
Differs from ‘intDiv’ in that it returns zero when dividing by zero or when dividing a minimal negative number by minus one.
Syntax
intDivOrZero(a, b)
Arguments
a
– The number.b
– The number.Returned value
Example
SELECT intDivOrZero(10, -2);
Result:
┌─intDivOrZero(10, -2)─┐ │ -5 │ └──────────────────────┘
Returns the least common multiple of the numbers.
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
Syntax
lcm(a, b)
Arguments
a
– The number.b
– The number.Returned value
Example
SELECT lcm(27,18);
Result:
┌─lcm(27, 18)─┐ │ 54 │ └─────────────┘
Aggregate function that calculates the minimum across a group of values.
Syntax
min(column)
Arguments
column
– The column name.Returned value
Example
CREATE TABLE test.test_min(id Int32) ENGINE = CnchMergeTree ORDER BY id; INSERT INTO test.test_min(id) VALUES(1),(2),(3),(4),(5); -- insert 1,2,3,4,5 to table SELECT min(id) FROM test.test_min;
Result:
┌─min(id)─┐ │ 1 │ └─────────┘
Calculates the difference. The result is always signed.
You can also calculate integer numbers from a date or date with time. The idea is the same – see above for ‘plus’.
Syntax
minus(a, b), a - b operator
Arguments
a
– The number.b
– The number.Returned value
a
and b
.Example
SELECT minus(10, 3);
Result:
┌─minus(10, 3)─┐ │ 7 │ └──────────────┘
Calculates the remainder after division.
If arguments are floating-point numbers, they are pre-converted to integers by dropping the decimal portion.
The remainder is taken in the same sense as in C++. Truncated division is used for negative numbers.
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
Syntax
modulo(a, b), a % b operator
Arguments
a
– The number.b
– The number.Returned value
Example
SELECT modulo(10, 3);
Result:
┌─modulo(10, 3)─┐ │ 1 │ └───────────────┘
Calculates the product of the numbers.
Syntax
multiply(a, b) # a * b operator
Arguments
a
– The number.b
– The number.Returned value
Example
SELECT multiply(3,12);
Result:
┌─multiply(3, 12)─┐ │ 36 │ └─────────────────┘
Calculates a number with the reverse sign. The result is always signed.
Syntax
negate(a) # -a operator
Arguments
a
– The number.Returned value
Example
SELECT negate(20);
Result:
┌─negate(20)─┐ │ -20 │ └────────────┘
Calculates the sum of the numbers.
You can also add integer numbers with a date or date and time. In the case of a date, adding an integer means adding the corresponding number of days. For a date with time, it means adding the corresponding number of seconds.
Syntax
select plus(a, b) # a + b operator
Arguments
a
– The number.b
– The number.Returned value
Example
select plus(1,2);
Result:
┌─plus(1, 2)─┐ │ 3 │ └────────────┘