This article summarizes the commonly used numerical calculation functions in SQL. The operations we can use in sql are nothing more than mathematics. The most basic four arithmetic operations (+-* /), and basic elementary functions.
Mathematical analysis classifies the basic elementary functions into six categories: power functions, exponential functions, logarithmic functions, trigonometric functions, inverse trigonometric functions, and constant functions.
It is summarized in a table for easy query.
Basic elementary functions | Features | For example | result |
---|---|---|---|
pow (x, y) | Power function x to the power of y | pow(2,3) | 8 |
exp(x) | Returns e to the power of x | exp(2) | 7.389056 |
sqrt(x) | Returns the square root of x | sqrt(9) | 3 |
cqrt(x) | Returns the square root of x | cbrt (8) | 2 |
log(x,y) | Logarithmic function value, x is the base, y is the true number | log(10,100) | 2 |
ln (x) | Returns the natural logarithm of x | ln(10) | 2.302585 |
sin (x) | Sine | without (0) | 0 |
cos(x) | Cosine | cos(0) | 1 |
so (x) | Tangent | tan(0) | 0 |
cot(x) | Cotangent | cot(1) | 0.6420926 |
asin(x) | Arc sine | asin(0) | 0 |
Commonly used functions | Features | For example | result |
---|---|---|---|
abs(x) | Returns the absolute value of x | abs(-1) | 1 |
mod (x, y) | Take the remainder | mod(5,3) | 2 |
ceil(x) | Returns the value of x rounded up | ceil(2.3) | 3 |
floor(x) | Returns the value of x rounded down | floor(2.3) | 2 |
round(x,k) | x retains k decimal places, rounded | round(2.345,2) | 2.35 |
truncate(x,k) | x retains k decimal places, using truncation method | truncate(2.345,2) | 2.34 |
sign() | Returns the sign of x | sign(-2) | -1 |
pi() | π constant | pi() | 3.141593 |
degrees(x) | Radians to angles | degrees(pi()) | 180 |
radians(x) | Angle to radians | radians(180) | 3.1415926 |
rand() | Generate a random number between 0 and 1 | rand() | 0.74609782 |
greatest(x_1, x2, x3,…) | Returns the maximum value | greatest(1,2,3,4,5) | 5 |
least(x1, x2, x3,…) | Returns the maximum value | least(1,2,3,4,5) | 1 |
Commonly used aggregate functions | Features |
---|---|
count() | Count the number of rows |
sum() | Return the sum |
avg() | Return average |
max() | Returns the maximum value |
min () | Return minimum |
备注:许多函数就是该操作的英文的简写,认识这些不会忘了
power→pow :n. 力量,能力;幂
squre root →sqrt : 平方跟 , cbrt : cube root →
ceilingn →ceil. 天花板;上限
floor →floor .地板;下限
round v. 变圆;四舍五入;
truncate v. 截断
modulo →mod希腊文取余的意思,据说高斯正式用来求余
degree n. 程度;度;学位
radians n. [数] 弧度