文章目录
- 一、日期函数
- 1、时间或日期截取函数(返回非日期)
- 2、时间或日期截取函数(返回日期)
- 3、日期或时间日期生成函数
- 二、类型转化类函数
- 1、精度保留(非四舍五入)
- 2、字符串转化为整数(非整数的字符串返回0)
- 3、日期与时间日期转化
- 4、转化为字符型
- 5、查看数据类型
- 三、字符串操作
- 1、基本字符串操作
- 2、字符串查找
- 3、字符串替换
- 4、字符串分割
- 5、字符串拼接
- 四、条件语句
- 五、数学函数
- 六、算术函数
- 七、舍入函数
- 八、URL操作函数
- 九、IP操作函数
- 十、表操作
- 1、表连接操作
- 2、limit操作
- 十一、字典操作
- 十二、聚合函数组合器
- 1、-If
- 2、-Merge
一、日期函数
1、时间或日期截取函数(返回非日期)
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
toYear() | 取日期或时间日期的年份 | toYear(toDateTime(‘2022-04-30 11:12:13’)) toYear(toDate(‘2022-04-30’)) | 2022 2022 |
toMonth() | 取日期或时间日期的月份 | toMonth(toDateTime(‘2022-04-30 11:12:13’)) toMonth(toDate(‘2022-04-30’)) | 4 4 |
toDayOfMonth() | 取日期或时间日期的天(1-31) | toDayOfMonth(toDayOfMonth(‘2022-04-30 11:12:13’)) toDayOfMonth(toDayOfMonth(‘2022-04-30’)) | 30 30 |
toDayOfWeek() | 取日期或时间日期的星期(星期一为1,星期日为7) | toDayOfWeek(toDateTime(‘2022-04-30 11:12:13’)) toDayOfWeek(toDate(‘2022-04-30’)) | 6 6 |
toHour() | 取时间日期的小时 | toHour(toDateTime(‘2022-04-30 11:12:13’)) | 11 |
toMinute() | 取时间日期的分钟 | toMinute(toDateTime(‘2022-04-30 11:12:13’)) | 12 |
toSecond() | 取时间日期的秒 | toSecond(toDateTime(‘2022-04-30 11:12:13’)) | 13 |
toMonday() | 取时间日期最近的周一(返回日期) | toMonday(toDate(‘2022-04-30’)) toMonday(toDateTime(‘2022-04-30 11:12:13’)) | 2022-04-25 2022-04-25 |
toTime() | 将时间日期的日期固定到某一天,保留原始时间 | toTime(toDateTime(‘2022-04-30 11:12:13’)) | 1970-01-02 11:12:13 |
2、时间或日期截取函数(返回日期)
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
toStartOfMonth() | 取日期或时间日期的月份的第一天,返回日期 | toStartOfMonth(toDateTime(‘2022-04-30 11:12:13’)) toStartOfMonth(toDate(‘2022-04-30’)) | 2022-04-01 2022-04-01 |
toStartOfQuarter() | 取日期或时间日期的季度的第一天,返回日期 | toStartOfQuarter(toDateTime(‘2022-04-30 11:12:13’)) toStartOfQuarter(toDate(‘2022-04-30’)) | 2022-04-01 2022-04-01 |
toStartOfYear() | 取日期或时间日期的年份的第一天,返回日期 | toStartOfYear(toDateTime(‘2022-04-30 11:12:13’)) toStartOfYear(toDate(‘2022-04-30’)) | 2022-01-01 2022-01-01 |
toStartOfMinute() | 截取时间日期到分钟(之后归零),返回日期 | toStartOfMinute(toDateTime(‘2022-04-30 11:12:13’)) | 2022-04-30 11:12:00 |
toStartOfFiveMinute() | 截取时间日期到最近的5的倍数分钟(之后归零),返回日期 | toStartOfFiveMinute(toDateTime(‘2022-04-30 11:12:13’)) | 2022-04-30 11:10:00 |
toStartOfFifteenMinutes() | 截取时间日期到最近的15的倍数分钟(之后归零),返回日期 | toStartOfFifteenMinutes(toDateTime(‘2022-04-30 11:12:13’)) | 2022-04-30 11:00:00 |
toStartOfHour() | 截取时间日期到小时(之后归零),返回日期 | toStartOfHour(toDateTime(‘2022-04-30 11:12:13’)) | 2022-04-30 11:00:00 |
toStartOfDay() | 截取时间日期到天(之后归零),返回日期 | toStartOfDay(toDateTime(‘2022-04-30 11:12:13’)) | 2022-04-30 00:00:00 |
timeSlot() | 将时间日期中,分钟大于等于30的归于30,分钟数小于30的归为00 | timeSlot(toDateTime(‘2022-04-30 11:12:13’)) timeSlot(toDateTime(‘2022-04-30 11:32:13’)) | 2022-04-30 11:00:00 2022-04-30 11:30:00 |
3、日期或时间日期生成函数
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
now() | 生成当前时间日期 | now() | 2022-04-30 11:12:13 |
today() | 生成今天的日期 | today() | 2022-04-30 |
yesterday() | 生成昨天的日期 | yesterday() | 2022-04-29 |
二、类型转化类函数
1、精度保留(非四舍五入)
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
toDecimal32() | 将数值型或者含有非数字的字符串进行精度保留 | toDecimal32(23.12291, 3) toDecimal32(‘_23.12291’, 3) | 23.122 0.000 |
toDecimal64() | 将数值型或者含有非数字的字符串进行精度保留 | toDecimal64(23.12291, 3) toDecimal64(‘_23.12291’, 3) | 23.122 0.000 |
toDecimal128() | 将数值型或者含有非数字的字符串进行精度保留 | toDecimal128(23.12291, 3) toDecimal64(‘_23.12291’, 3) | 23.122 0.000 |
2、字符串转化为整数(非整数的字符串返回0)
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
toUInt8OrZero() | 将无符号整数字符型转化为整数型,否则返回0 | toUInt8OrZero(‘123’) toUInt8OrZero(‘123.12’) | 123 0 |
toInt8OrZero() | 将整数字符型转化为整数型,否则返回0 | toInt8OrZero(‘123’) toInt8OrZero(‘-123’) | 123 -123 |
toFloat32OrZero() | 将数值字符串型转化为数值型,注意:从toFloat32OrZero开始,丢32的没有对应的函数 | toFloat32OrZero(‘-123’) toFloat32OrZero(‘123.123’) | -123 123.123 |
3、日期与时间日期转化
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
toDate() | 将字符型日期转化为日期型 | toDate(‘2022-04-30’) | 2022-04-30 |
toDateTime() | 将字符型时间日期转化为时间日期型 | toDateTime(‘2022-04-30 10:10:00’) | 2022-04-30 10:10:00 |
4、转化为字符型
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
toString() | 将数值型、字符型、日期等转化为字符型 | toString(‘2022-04-30’) toString(‘123’) | 2022-04-30 123 |
5、查看数据类型
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
toTypeName() | 返回数据的类型 | toTypeName(toString(‘123’)) toTypeName(toDate(‘2018-12-24’)) | String Date |
三、字符串操作
1、基本字符串操作
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
empty() | 判断字符串是空为1,否则为0 | empty(‘’) empty(‘123a’) | 1 0 |
notEmpty() | 判断字符串是非空为1,否则为0 | notEmpty(‘’) notEmpty(‘123a’) | 0 1 |
length() | 返回字符串的长度 | length(‘’) length(‘123a’) | 0 4 |
lower() | 将字符串转为小写 | lower(‘aBc’) | abc |
upper() | 将字符串转为大写 | upper(‘aBc’) | ABC |
reverse() | 将字符串反转 | reverse(‘abc’) | cba |
substring(s, offset, length) | 字符串截取 | substring(‘123abcABC’, 2, 3) | 23a |
appendTrailingCharIfAbsent(s, c) | 如果字符串s非空,则将s后追加一个字符c(s最后一个字符与c不同),否则不处理 | appendTrailingCharIfAbsent(‘123abc’, ‘b’) appendTrailingCharIfAbsent(‘123abc’, ‘c’) | 123abcb 123abc |
2、字符串查找
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
match(haystack,pattern) | 字符串正则匹配,返回0或1 | match(‘avhsca’,‘vh’) | 1 |
extract(haystack,pattern) | 返回匹配到的第一个子串 | extract(‘iioomAj12123124OOBJB’, ‘\d+’) | 12123124 |
extractAll(haystack,pattern) | 返回匹配到的所有子串,输出列表 | extractAll(‘iioomAj12123124OOBJ123B’, ‘\d+’) | [12123124,123] |
like(haystack,pattern) | 匹配到的数据返回1,否则返回0 | like(‘avhsca’,‘%vh%’) like(‘avhsca’,‘%vabjh%’) | 1 0 |
notLike(haystack, pattern) | 与like()函数相反 | notLike(‘avhsca’,‘%vh%’) notLike(‘avhsca’,‘%vabjh%’) | 0 1 |
3、字符串替换
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
replaceOne(haystack,pattern,replacement) | 替换第一个匹配到的pattern | replaceOne(‘asd123cbbj464sd’, ‘sd’, ‘-’) | a-123cbbj464sd |
replaceAll(haystack,pattern,replacement) | 替换所有匹配到的pattern | replaceAll(‘asd123cbbj464sd’, ‘sd’, ‘-’) | a-123cbbj464- |
replaceRegexpOne(haystack, pattern, replacement) | 正则匹配替换第一个匹配到的pattern | replaceRegexpOne(‘Hello, World!’, ‘o’, ‘-’) | Hell-, World! |
replaceRegexpAll(haystack,pattern,replacement) | 正则匹配替换所有匹配到的pattern | replaceRegexpAll(‘Hello, World!’, ‘o’, ‘-’) | Hell-, W-rld! |
4、字符串分割
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
splitByChar(separator, s) | 以单个字符分割字符串 | splitByChar(‘-’, ‘qw-asaf-asfqw-2312-asd’) | ['qw,‘asaf’,‘asfqw’,‘2312’,‘asd’] |
splitByString(separator, s) | 以单个或多个字符分割字符串 | splitByString(‘-’, ‘qw-asaf-asfqw-2312-asd’) splitByString(‘-a’, ‘qw-asaf-asfqw-2312-asd’) | [‘qw’,‘asaf’,‘asfqw’,‘2312’,‘asd’] [‘qw’,‘saf’,‘sfqw-2312’,‘sd’] |
5、字符串拼接
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
concat(s1,s2,…) | 将字符串拼接 | concat(‘123’, ‘abc’, ‘ABC’) | 123abcABC |
四、条件语句
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
if(cond,then,else) | 条件输出 | if(1 > 2, ‘正确’, ‘错误’) | 错误 |
multiIf(cond_1, then_1, cond_2, then_2…else) | 多条件输出 | multiIf(1 > 2, ‘正确’, 2 < 0, ‘正确’, ‘错误’) | 错误 |
五、数学函数
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
e() | 返回e的值 | e() | 2.718281828459045 |
pi() | 返回pi的值 | pi() | 3.141592653589793 |
exp(x) | 返回e的x次方 | exp(1) | 2.718281828459045 |
exp2(x) | 返回2的x次方 | exp2(2) | 4 |
exp10(x) | 返回10的x次方 | exp10(1) | 10 |
log(x) | 返回log以e为底的对数值 | log(e()) | 1 |
log2(x) | 返回log以2为底的对数值 | log2(2) | 1 |
log10(x) | 返回log以10为底的对数值 | log10(100) | 2 |
sqrt(x) | 对x开平方 | sqrt(4) | 2 |
cbrt(x) | 对x开立方 | cbrt(8) | 2 |
pow(x, y) | 返回x的y次方 | pow(2, 3) | 8 |
六、算术函数
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
plus(a, b) | 计算数值总和 | plus(50,1) | 51 |
minus(a, b) | 计算数值之差,结果总是有符号的 | minus(10,1) | 9 |
multiply(a, b) | 计算数值的乘积 | multiply(1, 1) | 1 |
divide(a, b) | 计算数值的商 ,结果类型始终是浮点类型。 | divide(1, 1) | 1 |
intDiv(a,b) | 计算数值的商,向下舍入取整(按绝对值),除以零或将最小负数除以-1时抛出异常。 | intDiv(1, 1) | 1 |
intDivOrZero(a,b) | 计算数值的商,向下舍入取整(按绝对值),与’intDiv’的不同之处在于它在除以零或将最小负数除以-1时返回零。 | intDivOrZero(1, 1) | 1 |
modulo(a, b) | 计算除法后的余数。除以零或将最小负数除以-1时抛出异常。 | modulo(1, 1) | 1 |
moduloOrZero(a, b) | 计算除法后的余数。除以零或将最小负数除以-1时抛出异常。和modulo不同之处在于,除以0时结果返回0。 | moduloOrZero(1, 1) | 0 |
negate(a) | 通过改变数值的符号位对数值取反,结果总是有符号的 | negate(1) | -1 |
abs(a) | 计算数值(a)的绝对值 | abs(-1) | 1 |
gcd(a,b) | 返回数值的最大公约数。除以零或将最小负数除以-1时抛出异常。 | gcd(12,3) | 3 |
lcm(a,b) | 返回数值的最小公倍数。除以零或将最小负数除以-1时抛出异常。 | lcm(12,3) | 12 |
七、舍入函数
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
floor(x[, N]) | 向下取数 | floor(123.883, 1) floor(123.883, -1) | 123.8 120 |
ceil(x[, N]) | 向上取数 | ceil(123.883, 1) ceil(123.883, -1) | 123.9 130 |
round(x[, N]) | 四舍五入 | round(123.883, 1) round(123.883, -1) | 123.9 120 |
更详细四舍五入见:ClickHouse 四舍五入函数
八、URL操作函数
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
protocol() | 返回URL的协议类型 | protocol(‘http://www.baidu.com.cn’) | http |
domain() | 返回URL的域名 | domain(‘http://www.baidu.com.cn’) | www.baidu.com.cn |
domainWithoutWWW() | 返回URL不带www的域名 | domainWithoutWWW(‘http://www.baidu.com.cn’) | baidu.com.cn |
topLevelDomain() | 返回顶级域名 | topLevelDomain(‘http://www.baidu.com.cn’) | cn |
firstSignificantSubdomain() | Returns the “first significant subdomain”. | firstSignificantSubdomain(‘http://www.baidu.com.cn’) | baidu |
cutToFirstSignificantSubdomain() | cutToFirstSignificantSubdomain(‘http://www.baidu.com.cn’) | baidu.com.cn | |
path() | 返回URL的路径 | path(‘https://www.baidu.com/s?wd=split’) | /s |
pathFull() | 返回URL的完整路径 | pathFull(‘https://www.baidu.com/s?wd=split’) | /s?wd=split |
queryString() | 返回URL的参数(查询字符串) | queryString(‘https://www.baidu.com/s?wd=split’) | wd=split |
extractURLParameters() | 以列表的形式返回URL的参数 | extractURLParameters(‘https://www.baidu.com/s?wd=split&name=tom’) | [‘wd=split’,‘name=tom’] |
extractURLParameterNames() | 以列表的形式返回URL的参数名 | extractURLParameterNames(‘https://www.baidu.com/s?wd=split&name=tom’) | [‘wd’,‘name’] |
cutQueryString() | 返回URL?(参数)前面的内容 | cutQueryString(‘https://www.baidu.com/s?wd=split&name=tom’) | https://www.baidu.com/s |
九、IP操作函数
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
IPv4StringToNum(s) | 将IPV4转为数值,非IPV4的转化为0 | IPv4StringToNum(‘23.217.198.69’) IPv4StringToNum(‘adwh.124.qwfqw’) | 400148037 0 |
IPv4NumToString(num) | 将数值转为IPV4 | IPv4NumToString(400148037) | 23.217.198.69 |
IPv4NumToStringClassC(num) | 将数值转为IPV4,且最后的段位用xxx代替 | IPv4NumToStringClassC(400148037) | 23.217.198.xxx |
十、表操作
1、表连接操作
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
INNER JOIN | 内连接 | select table_A.id from table_A inner join table_B on table_A.id = table_B.id | 返回 A表 与 B表 的公共部分 |
LEFT OUTER JOIN | 左外连接 | select table_A.id from table_A left outer join on table_A.id = table_B.id | 返回 A表 不在 B表 中的部分 |
RIGHT OUTER JOIN | 右外连接 | select table_A.id from table_A right outer join on table_A.id = table_B.id | 返回 B表 不在 A表 中的部分 |
FULL OUTER JOIN | 全外连接 | select table_A.id from table_A full outer join on table_A.id = table_B.id | 返回 A 与 B表 全部,没有为NULL |
2、limit操作
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
LIMIT N | 查询N条数据,一般跟ORDER BY 连用 | ORDER BY hit DESC LIMIT 10 | 按照hit列降序取前10条 |
LIMIT N BY Clause | 按照Clause列查询N条数据,一般跟ORDER BY 连用 | SELECT date, domain, count(1) AS hit from db.tb where…GROUP BY date, domain,ORDER BY hit DESCLIMIT 10 BY date | 取每天TOP10的域名 |
十一、字典操作
函数 | 说明 | 举例 | 结果 |
---|---|---|---|
dictGetString() | 字典映射 | dictGetString(‘ck_abc_dic’, ‘ck_value’, tuple(_abc)) |
十二、聚合函数组合器
1、-If
-If可以加到任何函数之后。加了 -If 之后聚合函数需要接受一个额外的参数,一个条件,如果条件满足,那聚合函数处理当前的数据,如果不满足,那返回默认值。