clickhouse 之常用函数


计算time1+time2<=3000的总个数

select countIf(time1+time2<=3000) from table

计算求a*b大于等于0的和

select  sumIf(a*b,a*b>=0) from table

查找列表中包含某个id的数据 ['aa','bb']

select * from table where  has(ids,'aa')

字符串转datetime类型

toDateTime('2022-02-23 11:49:00') 

求一个Date/DateTime时间减去时间差,再返回一个时间Date/DateTime

subtractSeconds(toDateTime('2019-10-30 16:29:00'), 120)

subtractYears,subtractMonths,subtractWeeks,subtractDays,subtractours,subtractMinutes,subtractSeconds,subtractQuarters都可以

求两个时间的时间差

SELECT dateDiff('hour', toDateTime('2018-01-01 22:00:00'), toDateTime('2018-01-02 23:00:00'));

转换为时间戳

toUnixTimestamp(datetime)
toUnixTimestamp(str, [timezone])

获取某个时间的整分 整点等。

toStartOfYear,toStartOfISOYear,toStartOfQuarter,toStartOfMonth,toMonday,toStartOfWeek(t[,mode]),toStartOfDay,toStartOfHour,toStartOfMinute,toStartOfSecond将datetime类型取整点

SELECT toStartOfMinute(toDateTime('2022-03-15 11:03:03')) 

查找同时不以多个字符串开头的数据,multiSearchAllPositions相当于查找某些字符在 当前的哪个位置,如果是1,则表示以这个text/ccs开头,如果是0,则表示在content-type中没找到

multiSearchAllPositionsCaseInsensitive可以不区分大小写搜索

select * from table where multiSearchAllPositions(content_type,['text/ccs','application/javascript','image/','font/'])==[0,0,0,0]