一、关键词
1. SQL 语法关键字(最常见)
2. 数据类型名(高频冲突)
二、聚合函数(Aggregate Functions)
作用:多行数据 → 单行结果(GROUP BY 必备)
1. 基础聚合
- count() / count(column):计数
- sum(x):求和
- avg(x):平均值
- min(x) / max(x):最小 / 最大值
- any(x) / anyLast(x):取任意一个值(优化快)
2. 去重与近似计算(性能优先)
- uniq(x):近似去重数(HyperLogLog)
- uniqExact(x):精确去重(大数据慢)
- uniqCombined(x):精度更高的近似去重
- quantile(0.9)(x):90 分位(近似)
- quantileExact(0.9)(x):精确分位
- median(x):中位数
3. 数组 / 列表聚合
- groupArray(x):分组转数组
- groupUniqArray(x):分组去重转数组
- groupBitmap(x):聚合为位图(高效去重计算)
4. 统计类
- stddevPop(x) / stddevSamp(x):总体 / 样本标准差
- covarPop(x,y):协方差
- corr(x,y):相关系数
三、日期时间函数
作用:DateTime / Date 类型处理
1. 取当前时间
- now() → DateTime
- today() → Date
- yesterday()
- toDateTime('2025-01-01 10:00:00')
2. 时间截断(分组常用)
- toStartOfHour(t) → 整点
- toStartOfDay(t) → 当天 0 点
- toStartOfMonth(t) → 当月 1 号
- toMonday(t) → 本周一
3. 时间加减
- addDays(t, 7) / subtractDays(t, 3)
- addHours(t, 2) / addMonths(t, 1)
4. 时间差
- dateDiff('day', t1, t2) → 天数差
- dateDiff('hour', t1, t2) → 小时差
5. 格式化
- formatDateTime(t, '%Y-%m-%d %H:%M:%S')
- toString(t, 'Asia/Shanghai') → 带时区
四、字符串函数
作用:文本处理、截取、匹配、拼接
- concat(s1, s2, ...)
- substring(s, offset, length)
- length(s) / lower(s) / upper(s)
- trim(s) / ltrim / rtrim
- replace(s, old, new)
- replaceRegexpAll(s, pattern, repl)
- splitByString(',', s) → 切为数组
- position(s, substr) → 子串位置
- match(s, pattern) → 正则匹配(返回 1/0)
- extract(s, pattern) → 正则提取第一个匹配
- extractAll(s, pattern) → 提取所有匹配
五、数组函数(Array)
ClickHouse 特色:数组能力极强
- array(1,2,3) → 构造数组
- length(arr) → 元素个数
- has(arr, 5) → 是否包含
- indexOf(arr, 5) → 位置
- arrayConcat(arr1, arr2)
- arraySlice(arr, 2, 3) → 切片
- arraySort(arr) → 排序
- arrayReverse(arr)
- arrayDistinct(arr) → 去重
- arrayFilter(x -> x>10, arr) → 过滤
- arrayMap(x -> x*2, arr) → 映射
- arraySum(arr) → 数组内求和
- arrayJoin(arr) → 行展开(炸裂)
六、条件与空值函数
逻辑判断、NULL 处理
- if(cond, a, b)
- multiIf(c1,a1, c2,a2, ..., default) → 多分支
- isNull(x) / isNotNull(x)
- coalesce(x1, x2, x3, ...) → 取第一个非 NULL
- ifNull(x, default)
- nullIf(a, b) → a==b 则返回 NULL
七、类型转换函数
强制类型转换
- toInt8(x) / toInt16 / toInt32 / toInt64
- toUInt8 ... toUInt64
- toFloat32 / toFloat64
- toString(x)
- toDate(x) / toDateTime(x)
- CAST(x AS Type) → 通用强转
八、数学函数
- abs(x)
- floor(x) / ceil(x) / round(x,2)
- exp(x) / log(x) / log10(x)
- sqrt(x) / pow(x,y)
- sin(x) / cos(x) / tan(x)
- rand() / rand64() → 随机数
九、JSON 函数(半结构化)
- JSONExtract(json, 'path') → 提取字段
- JSONExtractInt / String / Float / Array
- JSONHas(json, 'key')
- JSONLength(json)
十、字典与映射函数
- map('k1',v1, 'k2',v2) → 构造 Map
- mapContains(map, key)
- mapKeys(map)
- mapValues(map)
十一、窗口函数(Window Functions)
20.10+ 支持
- rowNumber()
- rank() / denseRank()
- lag(x,1) / lead(x,1)
- sum(x) OVER (PARTITION BY ... ORDER BY ...)
十二、用户行为 / 漏斗函数(特色)
用户路径、漏斗、留存专用
- windowFunnel(window)(time, c1,c2,c3...) → 漏斗计数
- sequenceMatch(pattern)(time, c1,c2...) → 路径匹配
- sequenceCount(pattern)(time, c1,c2...) → 满足路径的条数
- retention(c1,c2,...c32) → 留存矩阵
十三、哈希与加密
- hex(s) / unhex(s)
- md5(s) / sha1(s) / sha256(s)
- cityHash64(s) / murmurHash2_64(s) → 哈希分桶常用
十四、URL 函数
- protocol(url)
- domain(url)
- domainWithoutWWW(url)
- path(url)
- queryString(url)
- extractURLParameter(url, 'param')
十五、IP 函数
- IPv4ToNum(ip) → 转整数
- IPv4NumToString(num) → 转回字符串
- IPv4StringToNum(s)
- isIPv4String(s)
十六、其他常用
- greatest(a,b,c) → 取最大
- least(a,b,c) → 取最小
- hostName() → 服务器名
- database() → 当前库
- version() → CH 版本
- sleep(1) → 休眠 1 秒
是一站式前端监控 + 用户行为埋点 + 大数据分析平台,天然适配点位细查、用户行为回溯、批量导出等场景:
一体化架构:监控 + 埋点同一套 SDK,数据互通无壁垒
私有化部署:数据完全本地化,满足企业合规要求
高吞吐支撑:基于 ClickHouse 构建,亿级日志秒级查询
全端覆盖:H5 / 小程序 / APP / 鸿蒙全覆盖,统一导出口径
可定制强:支持接口扩展、分布式锁、限流降级等企业级能力