大家好!今天让小编来大家介绍下关于clickhouse日期函数的问题,以下是酷知号的小编对此问题的归纳整理,让我们一起来看看吧。
转载:日期函数
参考:ckag真人官方网址官网
1 clickhouse 时间日期函数 2 3 注:所有的时间日期函数都可以在第二个可选参数中接受时区参数。示例:asia / yekaterinburg。在这种情况下,它们使用指定的时区而不是本地(默认)时区。 4 5 select 6 todatetime'2016-06-15 23:00:00') as time, 7 todatetime) as date_local, 8 todatetime, 'asia/yekaterinburg') as date_yekat, 9 tostringtime, 'us/samoa') as time_samoa 10 11 ┌────────────────time─┬─date_local─┬─date_yekat─┬─time_samoa──────────┐ 12 │ 2016-06-15 23:00:00 │ 2016-06-15 │ 2016-06-16 │ 2016-06-15 09:00:00 │ 13 └─────────────────────┴────────────┴────────────┴─────────────────────┘ 14 15 16 常用时间函数: 17 18 now) // 2020-04-01 17:25:40 取当前时间 19 toyear) // 2020 取日期中的年份 20 tomonth) // 4 取日期中的月份 21 today) // 2020-04-01 今天的日期 22 yesterday) // 2020-03-31 昨天的额日期 23 todayofyear) // 92 取一年中的第几天 24 todayofweek) // 3 取一周中的第几天 25 tohour) //17 取小时 26 tominute) //25 取分钟 27 tosecond) //40 取秒 28 tostartofyear) //2020-01-01 取一年中的第一天 29 tostartofmonth) //2020-04-01 取当月的第一天 30 31 formatdatetimenow),'%y-%m-%d') // 2020*04-01 指定时间格式 32 toyyyymm) //202004 33 toyyyymmdd) //20200401 34 toyyyymmddhhmmss) //20200401172540 35 datediff) 36 ...... 37 38 select 39 todatetime'2019-07-30 10:10:10') as time, 40 41 -- 将datetime转换成unix时间戳 42 tounixtimestamptime) as unixtimestamp, 43 44 -- 保留 时-分-秒 45 todatetime) as date_local, 46 totimetime) as date_time, -- 将datetime中的日期转换为一个固定的日期,同时保留时间部分。 47 48 -- 获取年份,月份,季度,小时,分钟,秒钟 49 toyeartime) as get_year, 50 tomonthtime) as get_month, 51 52 -- 一年分为四个季度。1(一季度:1-3),2(二季度:4-6),3(三季度:7-9),4(四季度:10-12) 53 toquartertime) as get_quarter, 54 tohourtime) as get_hour, 55 tominutetime) as get_minute, 56 tosecondtime) as get_second, 57 58 -- 获取 datetime中的当前日期是当前年份的第几天,当前月份的第几日,当前星期的周几 59 todayofyeartime) as "当前年份中的第几天", 60 todayofmonthtime) as "当前月份的第几天", 61 todayofweektime) as "星期", 62 todatetime, 'asia/shanghai') as date_shanghai, 63 todatetimetime, 'asia/shanghai') as time_shanghai, 64 65 -- 得到当前年份的第一天,当前月份的第一天,当前季度的第一天,当前日期的开始时刻 66 tostartofyeartime), 67 tostartofmonthtime), 68 tostartofquartertime), 69 tostartofdaytime) as cur_start_daytime, 70 tostartofhourtime) as cur_start_hour, 71 tostartofminutetime) as cur_start_minute, 72 73 -- 从过去的某个固定的时间开始,以此得到当前指定的日期的编号 74 torelativeyearnumtime), 75 torelativequarternumtime); 76 77 获取未来时间的函数: 78 79 -- 第一种,日期格式(指定日期,需注意时区的问题) 80 with 81 todate'2019-09-09') as date, 82 todatetime'2019-09-09 00:00:00') as date_time 83 select 84 addyearsdate, 1) as add_years_with_date, 85 addyearsdate_time, 0) as add_years_with_date_time; 86 87 -- 第二种,日期格式(当前,本地时间) 88 with 89 todatenow)) as date, 90 todatetimenow)) as date_time 91 select 92 now) as now_time,-- 当前时间 93 -- 之后1年 94 addyearsdate, 1) as add_years_with_date, 95 addyearsdate_time, 1) as add_years_with_date_time, 96 97 -- 之后1月 98 addmonthsdate, 1) as add_months_with_date, 99 addmonthsdate_time, 1) as add_months_with_date_time, 100 101 --之后1周 102 addweeksdate, 1) as add_weeks_with_date, 103 addweeksdate_time, 1) as add_weeks_with_date_time, 104 105 -- 之后1天 106 adddaysdate, 1) as add_days_with_date, 107 adddaysdate_time, 1) as add_days_with_date_time, 108 109 --之后1小时 110 addhoursdate_time, 1) as add_hours_with_date_time, 111 112 --之后1分中 113 addminutesdate_time, 1) as add_minutes_with_date_time, 114 115 -- 之后10秒钟 116 addsecondsdate_time, 10) as add_seconds_with_date_time, 117 118 -- 之后1个季度 119 addquartersdate, 1) as add_quarters_with_date, 120 addquartersdate_time, 1) as add_quarters_with_date_time; 121 122 获取过去时间的函数: 123 124 with 125 todatenow)) as date, 126 todatetimenow)) as date_time 127 select 128 subtractyearsdate, 1) as subtract_years_with_date, 129 subtractyearsdate_time, 1) as subtract_years_with_date_time, 130 subtractquartersdate, 1) as subtract_quarters_with_date, 131 subtractquartersdate_time, 1) as subtract_quarters_with_date_time, 132 subtractmonthsdate, 1) as subtract_months_with_date, 133 subtractmonthsdate_time, 1) as subtract_months_with_date_time, 134 subtractweeksdate, 1) as subtract_weeks_with_date, 135 subtractweeksdate_time, 1) as subtract_weeks_with_date_time, 136 subtractdaysdate, 1) as subtract_days_with_date, 137 subtractdaysdate_time, 1) as subtract_days_with_date_time, 138 subtracthoursdate_time, 1) as subtract_hours_with_date_time, 139 subtractminutesdate_time, 1) as subtract_minutes_with_date_time, 140 subtractsecondsdate_time, 1) as subtract_seconds_with_date_time; 141 142 select todate'2019-07-31', 'asia/guangzhou') as date_guangzhou; 143 select todate'2019-07-31'), todate'2019-07-31', 'asia/beijing') as date_beijing; 144 145 -- 亚洲只能加载上海的timezone??? 146 select todatetime'2019-07-31 10:10:10', 'asia/shanghai') as date_shanghai; 147 148 计算连个时刻在不同时间单位下的差值 149 150 -- 第一种:指定时间计算差值示例 151 with 152 todatetime'2019-07-30 10:10:10', 'asia/shanghai') as date_shanghai_one, 153 todatetime'2020-10-31 11:20:30', 'asia/shanghai') as date_shanghai_two 154 select 155 datediff'year', date_shanghai_one, date_shanghai_two) as diff_years, 156 datediff'month', date_shanghai_one, date_shanghai_two) as diff_months, 157 datediff'week', date_shanghai_one, date_shanghai_two) as diff_week, 158 datediff'day', date_shanghai_one, date_shanghai_two) as diff_days, 159 datediff'hour', date_shanghai_one, date_shanghai_two) as diff_hours, 160 datediff'minute', date_shanghai_one, date_shanghai_two) as diff_minutes, 161 datediff'second', date_shanghai_one, date_shanghai_two) as diff_seconds; 162 163 -- 第二种:本地当前时间示例 164 with 165 now) as date_time 166 select 167 datediff'year', date_time, addyearsdate_time, 1)) as diff_years, 168 datediff'month', date_time, addmonthsdate_time, 2)) as diff_months, 169 datediff'week', date_time, addweeksdate_time, 3)) as diff_week, 170 datediff'day', date_time, adddaysdate_time, 3)) as diff_days, 171 datediff'hour', date_time, addhoursdate_time, 3)) as diff_hours, 172 datediff'minute', date_time, addminutesdate_time, 30)) as diff_minutes, 173 datediff'second', date_time, addsecondsdate_time, 35)) as diff_seconds;
1 ------------------------------------------------- 2 -------------------------------------------------- 3 ---------------clickhouse基础知识:函数学习------------- 4 --官址学习文档:https://clickhouse.yandex/docs/zh/ 5 -------------------------------------------------- 6 -------------------------------------------------- 7 8 9 10 11 ---------------------------------------------------------------------------- 12 -- dbeaver6.1.2操作快捷键: 13 -- 常用快捷键须知:(ctrl shift l 显示快捷键列表) 14 -- 1.删除光标所在行:ctrl d 15 -- 2.复制光标所在行:ctrl alt ↓ 16 -- 3.移动光标所在行:ctrl shift ↑/↓ 17 -- 4.缩小sql编辑器的文本字体大小:ctrl -/ 18 -- 5.查找:ctrl f 19 -- 6.选中上或下的sql执行语句:alt ↑/↓ 20 -- 7.执行当前光标所在sql语句(无论是否格式化过,前提是此sql语句和上一条有空行或者上一条sql语句有分号“;”隔开) 21 ---------------------------------------------------------------------------- 22 23 -- 零、检测函数类型(clickhouse中数据的类型) 24 select totypename0);-- uint8三位数为8) 25 select totypename-0);-- int8 26 select totypename-343);-- int16 27 select totypename12.43); -- float64默认浮点型的数据为64),所以一般在处理浮点型的数据的时候尽量转成tofloat3212.43) 28 select totypename12.34343); -- float64 29 select totypenametodatetime1502396027)); -- datetime 30 31 -- 一、算数函数 32 -->>>>>> 算数函数数学上的计算) 33 --求和 34 select plus12, 21), plus10, -10), plus-10, -10); 35 --差值 36 select minus10, 5), minus10, -10),minus-10, -10); 37 --积 38 select multiply12, 2), multiply12, -2), multiply-12, -2); 39 --平均值 40 select divide12, 4), divide10, 3), divide2, 4), divide-4, -2), divide-4, 2), divide-4.5, 3); 41 select intdiv10, 3), divide10, 3); -- 3, 3.333保留四位有效数字) 42 select divide10, 0), divide-10, 0); -- 出现无穷大字符“ ∞ ”或“ -∞ ” 43 select divide0, 0); -- 特殊字符(类似乱码) 44 select intdivorzero10, 0); -- 0 45 --求余数 46 select modulo10, 3); --1 47 select modulo10.5, 3); --1 48 --取反 49 select negate10), negate-10); -- -10 10 50 --绝对值 51 select abs-10), abs10); 52 --最大公约数 53 select gcd12, 24), gcd-12, -24), gcd-12, 24); 54 --最小公倍数 55 select lcm12, 24), lcm-12, -24), lcm-3, 4); 56 57 -- 二、比较函数 58 -->>>>>> 比较函数始终返回0表示false 或 1表示true) 59 select 12 == 12, 12 != 10, 12 == 132, 12 != 12, 12 <> 12; 60 select equals12, 12), notequals12, 10), equals12, 10), notequals12,123); 61 select greater12, 10), greater10, 12), greater12, 12);-- 前者是否大于后者 62 select greaterorequals12,10), greaterorequals12,12);-- 前者是否大于或等于后者 63 select less12, 21), less12, 10), less120, 120);-- 前者是否小于后者 64 select lessorequals12, 120), lessorequals12, 12);-- 前世是否小于或等于或者 65 66 -- 三、逻辑函数 67 -->>>>>> 逻辑操作符(返回0表示false 或 1表示true) 68 select 12==12 or 12!=10; 69 select 12==12 and 12!=10; 70 select not 12, not 0; 71 select orequals12, 12), notequals12, 10)); --函数表示法:或 72 select andequals12, 12), notequals12, 10));--函数表示法:且 73 select not12), not0); 74 75 -- 四、类型转换函数 76 -->>>>>> 类型转换函数部分示例: 77 select toint812.3334343), tofloat3210.001), tofloat641.000040); 78 select tostringnow)); 79 select now) as now_local, tostringnow), 'asia/yekaterinburg') as now_yekat; 80 select now) as now_local, todatenow)), todatetimenow)), tounixtimestampnow)); 81 82 select 83 '2016-06-15 23:00:00' as timestamp, 84 casttimestamp as datetime) as datetime, 85 casttimestamp as date) as date, 86 casttimestamp, 'string') as string, 87 casttimestamp, 'fixedstring22)') as fixed_string; 88 89 with 90 todate'2019-01-01') as date, 91 interval 1 week as interval_week, 92 tointervalweek1) as interval_to_week, 93 tointervalmonth1) as interval_to_month 94 select 95 date interval_week, 96 date interval_to_week, 97 date interval_to_month; 98 99 with 100 todatetime'2019-01-01 12:10:10') as datetime, 101 interval 1 hour as interval_hour, 102 tointervalhour1) as invterval_to_hour 103 select 104 plusdatetime, interval_hour), 105 plusdatetime, invterval_to_hour); 106 107 -- 五、时间日期函数 108 --->>>>>> 时间日期函数 109 select 110 todatetime'2019-07-30 10:10:10') as time, 111 -- 将datetime转换成unix时间戳 112 tounixtimestamptime) as unixtimestamp, 113 -- 保留 时-分-秒 114 todatetime) as date_local, 115 totimetime) as date_time,-- 将datetime中的日期转换为一个固定的日期,同时保留时间部分。 116 -- 获取年份,月份,季度,小时,分钟,秒钟 117 toyeartime) as get_year, 118 tomonthtime) as get_month, 119 -- 一年分为四个季度。1(一季度:1-3),2(二季度:4-6),3(三季度:7-9),4(四季度:10-12) 120 toquartertime) as get_quarter, 121 tohourtime) as get_hour, 122 tominutetime) as get_minute, 123 tosecondtime) as get_second, 124 -- 获取 datetime中的当前日期是当前年份的第几天,当前月份的第几日,当前星期的周几 125 todayofyeartime) as "当前年份中的第几天", 126 todayofmonthtime) as "当前月份的第几天", 127 todayofweektime) as "星期", 128 todatetime, 'asia/shanghai') as date_shanghai, 129 todatetimetime, 'asia/shanghai') as time_shanghai, 130 -- 得到当前年份的第一天,当前月份的第一天,当前季度的第一天,当前日期的开始时刻 131 tostartofyeartime), 132 tostartofmonthtime), 133 tostartofquartertime), 134 tostartofdaytime) as cur_start_daytime, 135 tostartofhourtime) as cur_start_hour, 136 tostartofminutetime) as cur_start_minute, 137 -- 从过去的某个固定的时间开始,以此得到当前指定的日期的编号 138 torelativeyearnumtime), 139 torelativequarternumtime); 140 141 select 142 todatetime'2019-07-30 14:27:30') as time, 143 toisoyeartime) as iso_year, 144 toisoweektime) as iso_week, 145 now) as cur_datetime1, -- 返回当前时间yyyy-mm-dd hh:mm:ss 146 today) as cur_datetime2, -- 其功能与'todatenow))'相同 147 yesterday) as yesterday, -- 当前日期的上一天 148 -- timeslot1) as timeslot_1, -- 出现异常!!将时间向前取整半小时 149 todatetime) as gety_m_d; 150 151 -- 目前只有这三种格式,没有什么toyyyy),toyyyddmm)之类的函数,不要想当然。 152 select 153 now) as nowtime, 154 -- 将date或datetime转换为包含年份和月份编号的uint32类型的数字(yyyy * 100 mm) 155 toyyyymmddhhmmssnowtime), 156 toyyyymmddnowtime), 157 toyyyymmnowtime); 158 159 -- formatdatetimetime, format[,timezone])函数引用 160 select 161 now) as now_time, 162 todatetime'2019-07-31 18:20:30') as def_datetime, 163 formatdatetimenow_time, '%d') as now_time_day_month_year,-- 07/30/19 164 -- todatetime'2019-07-31 18:20:30', 'asia/shanghai') as def_datetime1, -- 指定时区 165 formatdatetimedef_datetime, '%y') as def_datetime_year, -- 2019(指定日期为2019年) 166 formatdatetimedef_datetime, '%y') as def_datetime_year_litter, -- 19(指定日期为19年,year, last two digits 00-99),本世纪的第19年) 167 formatdatetimedef_datetime, '%h') as hour24, -- 18 下午六点 168 formatdatetimedef_datetime, '%i') as hour12, -- 06下午六点 169 formatdatetimedef_datetime, '%p') as pmoram, -- 指定时间是上午还是下午 170 formatdatetimedef_datetime, '%w') as def_datetime_get_curweek,-- 3指定日期为星期三) 171 formatdatetimedef_datetime, '%f') as def_datetime_get_date,-- 2019-07-31 172 formatdatetimedef_datetime, '%t') as def_datetime_get_time,-- 18:20:30 173 formatdatetimedef_datetime, '%m') as def_datetime_get_minute,-- 20得到指定事件的“分”,minute 00-59)) 174 formatdatetimedef_datetime, '%s') as def_datetime_get_second;-- 30得到指定事件的“秒”,second 00-59)) 175 176 177 178 -- 1.跳转到之后的日期函数 179 -- 第一种,日期格式(指定日期,需注意时区的问题) 180 with 181 todate'2019-09-09') as date, 182 todatetime'2019-09-09 00:00:00') as date_time 183 select 184 addyearsdate, 1) as add_years_with_date, 185 addyearsdate_time, 0) as add_years_with_date_time; 186 -- 第二种,日期格式(当前,本地时间) 187 with 188 todatenow)) as date, 189 todatetimenow)) as date_time 190 select 191 now) as now_time,-- 当前时间 192 addyearsdate, 1) as add_years_with_date,-- 之后1年 193 addyearsdate_time, 1) as add_years_with_date_time, 194 addmonthsdate, 1) as add_months_with_date,-- 之后1月 195 addmonthsdate_time, 1) as add_months_with_date_time, 196 addweeksdate, 1) as add_weeks_with_date,--之后1周 197 addweeksdate_time, 1) as add_weeks_with_date_time, 198 adddaysdate, 1) as add_days_with_date,-- 之后1天 199 adddaysdate_time, 1) as add_days_with_date_time, 200 addhoursdate_time, 1) as add_hours_with_date_time,--之后1小时 201 addminutesdate_time, 1) as add_minutes_with_date_time,--之后1分中 202 addsecondsdate_time, 10) as add_seconds_with_date_time,-- 之后10秒钟 203 addquartersdate, 1) as add_quarters_with_date, -- 之后1个季度 204 addquartersdate_time, 1) as add_quarters_with_date_time; 205 206 -- 2.跳转到当前日期之前的函数函数将date/datetime减去一段时间间隔,然后返回date/datetime) 207 with 208 todatenow)) as date, 209 todatetimenow)) as date_time 210 select 211 subtractyearsdate, 1) as subtract_years_with_date, 212 subtractyearsdate_time, 1) as subtract_years_with_date_time, 213 subtractquartersdate, 1) as subtract_quarters_with_date, 214 subtractquartersdate_time, 1) as subtract_quarters_with_date_time, 215 subtractmonthsdate, 1) as subtract_months_with_date, 216 subtractmonthsdate_time, 1) as subtract_months_with_date_time, 217 subtractweeksdate, 1) as subtract_weeks_with_date, 218 subtractweeksdate_time, 1) as subtract_weeks_with_date_time, 219 subtractdaysdate, 1) as subtract_days_with_date, 220 subtractdaysdate_time, 1) as subtract_days_with_date_time, 221 subtracthoursdate_time, 1) as subtract_hours_with_date_time, 222 subtractminutesdate_time, 1) as subtract_minutes_with_date_time, 223 subtractsecondsdate_time, 1) as subtract_seconds_with_date_time; 224 225 select todate'2019-07-31', 'asia/guangzhou') as date_guangzhou; 226 select todate'2019-07-31'), todate'2019-07-31', 'asia/beijing') as date_beijing; 227 -- 亚洲只能加载上海的timezone??? 228 select todatetime'2019-07-31 10:10:10', 'asia/shanghai') as date_shanghai; 229 230 231 -- 计算连个时刻在不同时间单位下的差值 232 -- 第一种:指定时间计算差值示例 233 with 234 todatetime'2019-07-30 10:10:10', 'asia/shanghai') as date_shanghai_one, 235 todatetime'2020-10-31 11:20:30', 'asia/shanghai') as date_shanghai_two 236 select 237 datediff'year', date_shanghai_one, date_shanghai_two) as diff_years, 238 datediff'month', date_shanghai_one, date_shanghai_two) as diff_months, 239 datediff'week', date_shanghai_one, date_shanghai_two) as diff_week, 240 datediff'day', date_shanghai_one, date_shanghai_two) as diff_days, 241 datediff'hour', date_shanghai_one, date_shanghai_two) as diff_hours, 242 datediff'minute', date_shanghai_one, date_shanghai_two) as diff_minutes, 243 datediff'second', date_shanghai_one, date_shanghai_two) as diff_seconds; 244 245 -- 第二种:本地当前时间示例 246 with 247 now) as date_time 248 select 249 datediff'year', date_time, addyearsdate_time, 1)) as diff_years, 250 datediff'month', date_time, addmonthsdate_time, 2)) as diff_months, 251 datediff'week', date_time, addweeksdate_time, 3)) as diff_week, 252 datediff'day', date_time, adddaysdate_time, 3)) as diff_days, 253 datediff'hour', date_time, addhoursdate_time, 3)) as diff_hours, 254 datediff'minute', date_time, addminutesdate_time, 30)) as diff_minutes, 255 datediff'second', date_time, addsecondsdate_time, 35)) as diff_seconds; 256 257 -- timeslotstarttime, duration, [,size]) 258 -- 它返回一个时间数组,其中包括从从“starttime”开始到“starttime duration 秒”内的所有符合“size”(以秒为单位)步长的时间点 259 -- 作用:搜索在相应会话中综合浏览量是非常有用的。 260 select 261 timeslotstodatetime'2012-01-01 12:20:00'), touint32600)) as datetimearray, 262 datetimearray[0] as arr_index_0, -- no result. 263 datetimearray[1] as arr_index_1, -- 2012-01-01 20:00:00 264 datetimearray[2] as arr_index_2, -- 2012-01-01 20:30:00 265 datetimearray[3] as arr_index_3, -- no result. 266 datetimearray[4] as arr_index_4; -- no result. 267 -- touint32600) 表示之后间距20秒的时刻 268 select 269 timeslotsnow), touint32600), 20) as datetimearray, -- 类似于:引用地址 270 datetimearray[0] as arr_index_0, -- no result.为什么? 271 datetimearray[1] as arr_index_1, 272 datetimearray[2] as arr_index_2, 273 datetimearray[3] as arr_index_3, 274 datetimearray[4] as arr_index_4, 275 datetimearray[5] as arr_index_5; 276 -- 指定时间为基准,之后每个元素增加20秒 277 select 278 timeslotstodatetime'2012-01-01 12:20:00'), touint32600), 20) as cur_datetimearray, -- 类似于:引用地址 279 cur_datetimearray[0] as arr_index_0, -- no result.为什么? 280 cur_datetimearray[1] as arr_index_1, -- 2012-01-01 20:20:00 281 cur_datetimearray[2] as arr_index_2, -- 2012-01-01 20:20:20 282 cur_datetimearray[3] as arr_index_3, -- 2012-01-01 20:20:40 283 cur_datetimearray[4] as arr_index_4, -- 2012-01-01 20:21:00 284 cur_datetimearray[5] as arr_index_5; -- 2012-01-01 20:21:20 285 286 287 -- 六、字符串函数 288 --->>>>>> 字符串函数: 289 select 290 length'hello world') as str_length, -- 按照unicode编码计算长度“你好”的长度为6 291 empty'hello world'),-- 判断字符串是否为空,空为1,非空为0 292 notempty'hello world'), 293 lengthutf8'hello world'), -- 按照实际字符计算长度“你好”为2 294 char_length'hello world'), -- 同 lengthutf8) 295 character_length'hello world'), -- 同 lengthutf8), 296 lower'abcd123--'),--字母全部小写(将字符串中的ascii转换为小写。) 297 upper'abcd123--'),--字母全部大写(将字符串中的ascii转换为大写。) 298 lowerutf8'abcd123-/*8asd-\'), -- abcd123-/*8asd- 299 upperutf8'abcd123--'), -- abcd123-- 300 isvalidutf8'abcd123--/**'); --检查字符串是否为有效的utf-8编码,是则返回1,否则返回0。 301 select notempty''), notemptynull), notempty'he'); -- 0,空,1 302 select tovalidutf8'x61xf0x80x80x80b'); 303 -- reverseutf8):以unicode字符为单位反转utf-8编码的字符串。如果字符串不是utf-8编码,则可能获取到一个非预期的结果(不会抛出异常) 304 select reverse'abcdefg'), reverseutf8'abcdefg'); 305 -- 2.字符串维度自定义安排 306 select format'{1} {0} {1}', 'world', 'hello'); -- 输出:hello world hello 307 select format'{0} {0} {1} {1}', 'one', 'two'); -- 输出:one one two two 308 select format'{} {}', 'hello', 'world'); -- 输出:hello world 309 -- 3.字符串拼接 concats1,s2,s3,...) 310 select concat'hello',' ','world', '!');-- hello world! 311 -- 与concat相同,区别在于,你需要保证concats1, s2, s3) -> s4是单射的,它将用于group by的优化。 312 select concatassumeinjective'hello',' ','world', '!');-- hello world! 313 -- 4.字符串截取:substrings, offset, length), mids, offset, length), substrs, offset, length) 314 -- 以字节为单位截取指定位置字符串,返回以‘offset’位置为开头,长度为‘length’的子串。‘offset’从1开始(与标准sql相同)。‘offset’和‘length’参数必须是常量。 315 select 316 substring'abcdefg', 1, 3),-- abc 317 substring'你好,世界', 1, 3),-- 你 318 substringutf8'你好,世界', 1, 3); -- 你好, 319 -- 5.字符串拼接:appendtrailingcharifabsents, c) 320 -- 如果‘s’字符串非空并且末尾不包含‘c’字符,则将‘c’字符附加到末尾。 321 select 322 appendtrailingcharifabsent'good','c'), -- goodc 323 appendtrailingcharifabsent'goodccc','c'); -- goodccc 324 -- 6.字符串编码转换:convertcharsets, from, to) 返回从‘from’中的编码转换为‘to’中的编码的字符串‘s’。 325 select 326 convertcharset'hello', 'utf8','unicode'),-- ��h 327 convertcharset'hello', 'unicode', 'utf8'),-- 桥汬� 328 convertcharset'hello', 'unicode', 'ascii'),-- 329 convertcharset'hello', 'ascii', 'ascii'),--hello 330 convertcharset'hello', 'utf8','utf8');-- hello 331 select 332 base64encode'username password'),-- dxnlcm5hbwurcgfzc3dvcmq= 333 base64decode'dxnlcm5hbwurcgfzc3dvcmq='), -- username password 334 -- 使用base64将字符串解码成原始字符串。但如果出现错误,将返回空字符串。 335 trybase64decode'dxnlcm5hbwurcgfzc3dvcmq='); 336 -- 7.判断字符串是否已什么结尾或结束,返回1:true,0:flase 337 -- endswiths, suffix) 返回是否以指定的后缀结尾。如果字符串以指定的后缀结束,则返回1,否则返回0 338 -- startwiths, prefix) 返回是否以指定的前缀开头。如果字符串以指定的前缀开头,则返回1,否则返回0。 339 select 340 endswith'string','g'), 341 startswith'string', 'str'); -- 1 true 342 -- 8.删除左侧空白字符 343 -- trimlefts) 返回一个字符串,用于删除左侧的空白字符 344 -- trimrights) 返回一个字符串,用于删除右侧的空白字符 345 -- trimboths) 返回一个字符串,用于删除左侧和右侧的空白字符 346 select 347 trimleft' sdfdgs'), -- sdfdgs 348 trimright'abcd '), -- abcd 349 trimboth' abcd '); -- abcd 350 351 -- 七、字符串搜索函数 352 --->>>>>> 字符串搜索函数 353 -- pasitionhaystack, needle), 显示needle在haystack的第一个出现的位置。 354 select 355 position'2121stringstrstrstrstr','str') as positionsearch, -- 5 356 position'你好,hello,12323-你好,你,好sdfd*dg', '你,好'),-- 31 357 positionutf8'n12你好','你好') as positionutf8,-- 4 358 positioncaseinsensitive'abcdcdefabcd','bc') as positioncaseinsensitive, --2 359 locate'hellohellohellohello','ello'); -- 2 360 -- multisearchallpositionshaystack, [needle1, needle2, ..., needlen]) 361 -- 注意:在所有multisearch*函数中,由于实现规范,needles的数量应小于2^8。 362 -- 函数返回一个数组,其中包含所有匹配needlei的位置 363 select 364 multisearchallpositions'goodnamegoodnamegoodhellohihihi', ['dn', 'good']) as multisearch,-- [4,1] 365 multisearchallpositionscaseinsensitive'namessdfagpssdfdfetgfderef', ['ss','fa']) as multicaseinsensitive, 366 multisearchallpositionsutf8'namessdfazz轴功率gpssdfdfetgfderef', ['ss','fa', 'zz轴']) as multisearchutf8, 367 multisearchallpositionscaseinsensitiveutf8'namessdfazz轴功率gpssdfdfetgfderef', ['ss','fa', 'zz轴']) as multicaseinsensitiveutf8; 368 -- 检查字符串是否与pattern正则表达式匹配。pattern可以是一个任意的re2正则表达式。 re2正则表达式的语法比perl正则表达式的语法存在更多限制。 369 -- matchhaystack, pattern) 匹配到了则返回1,否则返回0 370 select 371 match'1232434sadgaddfsrefds', '[0-9a-za-z]'), -- 存在匹配的字符,返回1 372 match'1232321', '[a-z]'); -- 不存在匹配的字符,返回0 373 -- 与match相同,但如果所有正则表达式都不匹配,则返回0;如果任何模式匹配,则返回1。它使用hyperscan库。对于在字符串中搜索子字符串的模式,最好使用“multisearchany”,因为它更高效。 374 -- multimatchanyhaystack, [pattern1, pattern2, ..., patternn]) 375 -- 注意:任何haystack字符串的长度必须小于232字节,否则抛出异常。这种限制是因为hyperscan api而产生的。 376 -- 多个正则表达式对原始字符进行匹配,如若只有一个正则表达式匹配上了则返回1,否则返回0 377 select 378 multimatchany'abcabc',['[0-9]','[a-za-z]']) as multimatchanyone, -- 1 379 multimatchany'123abcabc',['[0-9]','[a-za-z]']) as multimatchanytwo, --1 380 -- 与multimatchany相同,但返回与haystack匹配的任何内容的索引位置。 381 multimatchanyindex'123abcabc', ['[0-9]','[a-za-z]']) as multimatchanyindex; --2 382 -- 模糊匹配:like)函数,注意大写敏感。 383 -- % 表示任何字节数(包括零字符) 384 -- _ 表示任何一个字节 385 select 386 'hello' like '%h%' as like_up, -- 1 387 'hello' like 'he' as like_low, -- 0 388 'hello' not like 'he' as not_like, -- 1 389 'hello' like '%he%' as like_litter, -- 1 390 like'adgadgadfa1232', '_12_') as like_func, 391 like'sdfasdfasd', '[a-z]') as like_func2, -- 0 392 notlike'1232423', '[a-za-z]') as not_like_func; -- 1 393 -- 使用字符串截取字符串:extracthaystack, pattern) 394 -- 使用正则表达式截取字符串。如果‘haystack’与‘pattern’不匹配,则返回空字符串。如果正则表达式中不包含子模式,它将获取与整个正则表达式匹配的子串。否则,它将获取与第一个子模式匹配的子串。 395 select 396 extractall'hellogoodaimantideaideafasd123232', '[0-9]'), -- ['1','2','3','2','3','2'] 397 extractall'12323dsdfre', '[a-z]'),-- ['s','d','f','r','e'] 398 extract'helloclickhouse', '[a-z]');-- h 399 -- ngramsearchhaystack, needle) 400 -- 基于4-gram计算haystack和needle之间的距离:计算两个4-gram集合之间的对称差异,并用它们的基数和对其进行归一化。 401 -- 返回0到1之间的任何浮点数 -- 越接近0则表示越多的字符串彼此相似。 402 -- 如果常量的needle或haystack超过32kb,函数将抛出异常。如果非常量的haystack或needle字符串超过32kb,则距离始终为1。 403 select 404 ngramdistance'hello123456789','123') as ngramdistance, 405 ngramdistancecaseinsensitive'hello123456789','123') as ngramdistancecaseinsensitive, 406 ngramdistanceutf8'hello123456789','123') as ngramdistanceutf8, 407 ngramdistancecaseinsensitiveutf8'hello123456789','123') as ngramdistancecaseinsensitiveutf8; 408 -- 注意:对于utf-8,我们使用3-gram。所有这些都不是完全公平的n-gram距离。 409 -- 我们使用2字节哈希来散列n-gram,然后计算这些哈希表之间的(非)对称差异 - 可能会发生冲突。 410 -- 对于utf-8不区分大小写的格式,我们不使用公平的tolower函数 411 -- 我们将每个unicode字符字节的第5位(从零开始)和字节的第一位归零 412 -- 这适用于拉丁语,主要用于所有西里尔字母。 413 414 --八、字符串替换函数 415 --->>>>>> 字符串替换函数 416 -- 替换匹配到的字符串 417 -- replaceonehaystack, pattern, replacement) 418 -- 用‘replacement’子串替换‘haystack’中与‘pattern’子串第一个匹配的匹配项(如果存在)。 ‘pattern’和‘replacement’必须是常量。 419 -- replaceallhaystack, pattern, replacement), replacehaystack, pattern, replacement) 420 -- 用‘replacement’子串替换‘haystack’中出现的所有‘pattern’子串。 421 select 422 replaceone'hed1234544', '4', '*') as replaceone,-- hed123*544 423 replaceregexpone'hed1234544', '4', '*') as replaceregexpone,-- hed123*544 424 replace'hed1234544', '4', '*') as replace, -- hed123*5** 425 replaceall'hed1234544', '4', '*') as replaceall;-- hed123*5** 426 427 -- 实例:2019-07-31 改变成 07/31/2019 428 select 429 todatenow)) as now_date, 430 replaceregexponetostringnow_date), '\d{4})-\d{2})-\d{2})', '\2/\3/\1') as format_date; 431 -- 示例:赋值字符串10次 432 select replaceregexpone'hello, world!', '.*', '\0\0\0\0\0\0\0\0\0\0') as res; 433 -- replaceregexpallhaystack, pattern, replacement) 434 -- 与replaceregexpone相同,但会替换所有出现的匹配项。例如: 435 select replaceregexpall'hello,world!', '.', '\0\0') as res; -- hheelllloo,,wwoorrlldd!! 436 select replaceregexpall'hello o o, world.', ' ', '*') as res; -- hello*o*o,*world. 437 438 -- 函数:regexpquotemetas) 该函数用于在字符串中的某些预定义字符之前添加反斜杠。 439 -- 预定义字符:'0','','|','',')','^','$','。','[',']','?','* ','','{',':',' - '。 440 -- 这个实现与re2 :: re2 :: quotemeta略有不同。它以而不是x00转义零字节,它只转义所需的字符 441 ---- 简言之,就是不处理转义字符,一般如果没有用的这个函数,都会有转义的情况出现。 442 select regexpquotemeta'\\|[]{}_-=@!~`&^*%$#'); -- \\|[]{} _-=@!~`&^*%$# 443 select tostring'\\'); -- \ 444 445 446 --九、条件函数 447 --->>>>>> 条件函数 448 -- 1. ifcond, then, else)函数:类似于三元操作符。 449 -- 中文字符使用双引号,英文字符可不使用引号也可使用当引号或双引号,根据具体情况而定。 450 -- 如果cond != 0则返回then,如果cond = 0则返回else。 cond必须是uint8类型,then和else必须存在最低的共同类型。 451 -- 注意:then和else可以是null 452 select 453 12 > 10 ? 'desc' : 'asc' as "三元操作符", 454 if12 > 10, 'desc' , 'asc') as "if)函数", 455 if12 > 10, null, null); 456 -- 2. multiifcond_1, then_1, cond_2, then_2...else) 457 -- 允许您在查询中更紧凑地编写case运算符。类似于java中的switch语法(可以接受2n 1个参数) 458 select multiif1,'one',2,'two',3,'three','not this index');-- 关联case条件表达式 459 460 --十、数学函数 461 --->>>>>> 数学函数 462 select 463 1 * e) as e, 464 1 * pi) as pi, 465 sqrt25) as sqrt_25, --接受一个数值类型的参数并返回它的平方根。 466 cbrt27) as cbrt_27, --接受一个数值类型的参数并返回它的立方根。 467 exp10), --接受一个数值类型的参数并返回它的指数 468 exp1010), --接受一个数值类型的参数并返回它的10的x次幂。 469 log10) as log, 470 log210) as log2, --接受一个数值类型的参数并返回它的底2对数。 471 lne)) as log10; --接受一个数值类型的参数并返回它的自然对数 472 -- 示例:三西格玛准则 473 select erf3 / sqrt2)); -- 0.997 474 select 475 sin90), -- 返回x的三角正弦值。 476 cos90), -- 返回x的三角余弦值。 477 tan90), -- 返回x的三角正切值 478 acos0), -- 返回x的反三角余弦值。 479 asin1), -- 返回x的反三角正弦值。 480 atan45); -- 返回x的反三角正切值。 481 -- powx, y), powerx, y) 接受x和y两个参数。返回x的y次方。 482 select 483 pow2, 3), -- 2的三次方 484 pow3, 2); -- 3的平方 485 select 486 intexp24), --2^4 接受一个数值类型的参数并返回它的2的x次幂(uint64)。 487 intexp102);--10^2 接受一个数值类型的参数并返回它的10的x次幂(uint64)。 488 489 -- 十一、取整函数 490 --->>>>>> 取整函数 491 -- 1.向下取整:floorx[,n]) 492 select 493 floortofloat3212.08098), 2), -- 12.08 494 floortofloat3212.2323), 2), -- 12.23 495 floortofloat3212.89788), -1), -- 10 496 floortofloat3212.09590), 3), -- 12.095 注意:如果按照正常的四舍五入,则应该是12.096,为什么呢?) 497 floortofloat3212.0987), 3),-- 12.098 498 floor10, 2); -- 10 499 -- 2.四舍五入:roundexpression [, decimal_places]) 500 -- 如果decimal_places=0,则取整数; 501 -- 如果>0,则将值舍入小数点右侧; 502 -- 如果<0,则将小数点左侧的值四舍五入。 503 select 504 roundtofloat3212.1234), 3), 505 roundtofloat3212.0025), 3), -- 12.002注意:为什么不是12.003呢?) 506 -- round函数只会最多保留三位有效数字 507 roundtofloat3212.0025), 4), -- 12.002 508 roundtofloat3212.0025002323), 100); -- 12.003 509 -- 示例: 510 select 511 roundtofloat3210 / 3)), -- 3 512 roundtofloat3210 / 3), 2), -- 3.33 513 roundtofloat3210.000/3), 3), -- 3.333 514 roundtofloat3210.000/3), 6); -- 3.333 515 -- roundtoexp2) 接受一个数字。如果数字小于1,则返回0。否则,它将数字向下舍入到最接近的(整个非负)2的x次幂。 516 select 517 roundtoexp212.0129), -- 8 = 2^3 518 roundtoexp2tofloat320.01)); -- 0.008 519 -- 3.向上取整:ceilx[, n]) 或者 ceilingx[, n]) 520 select 521 ceil12.34343, 3), -- 12.344 522 ceiltofloat6412.34343), 3), -- 12.344 523 ceiltofloat3212.34343), 3), -- 12.344 524 ceil12.0011, 3); -- 12.002 525 526 527 ---十二、数组函数 528 --->>>>>> 数组函数 529 -- 1.数组非空判断相关函数(真为1,假为0) 530 select empty[]), empty[1,2,3]), notempty[1,2,3]), notempty[]); 531 -- 2.数组长度 length) 返回数组中的元素个数。 结果类型是uint64。 该函数也适用于字符串。 532 select 533 -- length), -- 出现异常 534 -- length[true, false]), -- 异常 535 -- length[1,2,,4]), --出现异常! 536 length[]), -- 0 537 length['a','b','c']), -- 3 538 length[1,2,3]); -- 3 539 -- 3.扩展判断非空的部分函数如下:不接受任何参数并返回适当类型的空数组 540 select 541 emptyarrayuint8), -- uint8的空数组 542 emptyarrayuint16), 543 emptyarrayuint32), 544 emptyarrayuint64), 545 emptyarraydate), 546 emptyarraydatetime), 547 emptyarrayint8), 548 emptyarrayint16), 549 emptyarrayint32), 550 emptyarrayint64); 551 -- 接受一个空数组并返回一个仅包含一个默认值元素的数组。以下是部分示例) 552 select 553 emptyarraytosingleemptyarrayint32)), -- 0 554 emptyarraytosingleemptyarrayuint32)), -- 0 555 emptyarraytosingleemptyarraydate)), -- 0002-11-30 556 emptyarraytosingleemptyarraydatetime)); --0002-11-30 08:00:00 557 -- 4.生成一个含有n个元素的数组,元素从0开始增长,步长尾1. 558 -- rangen) 返回从0到n-1的数字数组。 以防万一,如果在数据块中创建总长度超过100,000,000个元素的数组,则抛出异常 559 select 560 range10), -- [0,1,2,3,4,5,6,7,8,9] 561 range2), -- [0,1] 562 -- range5.5), -- 出现异常,n为int8的数据类型,正整数 563 -- range-10), -- 出现异常,db::exception: illegal type int8 of argument of function range 564 range1); -- 0 565 -- 5.新建一个数组的函数:arrayx1,……) 类似于 直接[x1,……] 566 -- 注意:新建数组的每个元素的数据类型需保持一致性。 567 select 568 array1,2,2,3,4) as "array)函数", 569 -- [1,'hello',3], -- 出现异常,db::exception: there is no supertype for types uint8, string, uint8 because some of them are string/fixedstring and some of them are not version 19.10.1.5 official build)) 570 [1,2,3,4] as "[ ]"; 571 -- 6.合并n个数组 arrayconcatarrays) 合并参数中传递的所有数组。跟java的数组差不多的合并,不会自动去重,不会自动排序 572 select 573 arrayconcatarray1,2),array2,3),array4,5)), -- [1,2,2,3,4,5](第一种情况) 574 arrayconcatarray1,1),array2,2),array3,3)), -- [1,1,2,2,3,3] 575 -- arrayconcatarray1,2),['a','c'],array3,3)), -- 出现异常,不能将不同类型的数组进行合并 576 arrayconcatarray1,1),[2,3],array4,5)); -- [1,1,2,3,4,5] 577 -- 7.从数组arr中获取索引为“n”的元素。 578 -- n必须是任何整数类型。 数组中的索引从一开始。 支持负索引。在这种情况下,它选择从末尾开始编号的相应元素。例如,arr [-1]是数组中的最后一项。 579 -- 如果索引超出数组的边界,则返回默认值(数字为0,字符串为空字符串等). 580 select 581 arrayelementarray10,20,3), 1), -- 10 582 arrayelementarray1,20,3), 2), -- 20 583 arrayelementarray1,2,30), 3), -- 30 584 arrayelementarray10,20,3), 0), -- 0 585 arrayelementarray10,20,3), -3), -- 10 586 arrayelementarray10,20,3), -2), -- 20 587 arrayelementarray10,20,3), -1);-- 3 588 -- 8.检查在数组中是否含有此元素。hasarr, elem) 包含此元素则返回1,否则返回0 589 -- has) 检查'arr'数组是否具有'elem'元素。 如果元素不在数组中,则返回0;如果在,则返回1。 590 -- hasanyarr1, arr2) 返回1表示arr1和arr2存在交集。否则返回0. 591 --注意:特殊的定义: 592 -- ① “null”作为数组中的元素值进行处理。 593 -- ② 忽略两个数组中的元素值的顺序 594 -- hasallset, subset) 检查一个数组是否是另一个数组的子集。返回1,表示set包含subset中所有的元素 595 -- set – 具有一组元素的任何类型的数组。 596 -- subset – 任何类型的数组,其元素应该被测试为set的子集。 597 -- 注意:特殊的定义: 598 -- ① 空数组是任何数组的子集。 599 -- ② “null”作为数组中的元素值进行处理。 600 -- ③ 忽略两个数组中的元素值的顺序。 601 select 602 has[1,2,3], 2), -- 1 603 hasarray1,2,3),2), -- 1 604 has[1,2,null], null), -- 1 注意:null值的处理) 605 -- has[], 2), -- 出现异常,db::exception: types of array and 2nd argument of function has must be identical up to nullability or numeric types or enum and numeric type. passed: arraynothing) and uint8 606 has[1,2], 3); -- 0 607 select 608 hasall[], []), -- 1 609 hasall[1,null,null], [null]), -- 1 610 hasall[1,2,3], [1,2]), -- 1 611 hasall[1,2,2,3], [2]), -- 1 612 hasallarray1,2,2,3), [2]), -- 1 613 hasall[1,2,3], [4,5]); -- 0 614 -- 多重数组(如下的二维数组)。 615 select hasall[[1, 2], [3, 4]], [[1, 2], [3, 5]]); -- 0 616 select 617 hasanyarray1,2,3), array1)), -- 1 618 hasanyarray1,2,3), array1,4,56,80)), -- 1 619 -- []与array)是一样的含义,本质上是一直的。只不过[]更加简便而已。 620 hasanyarray), array)), -- 0 621 hasany[],[]), -- 0 622 hasany[1],[]), -- 0 623 -- 空数组跟null不是一样的对象 624 hasany[1,null],[]), -- 0 625 hasany[1,null],[null,2]); -- 1 626 627 628 -- 9.返回数组指定元素的索引 629 -- indexofarr, x) 返回数组中第一个‘x’元素的索引(从1开始),如果‘x’元素不存在在数组中,则返回0。 630 select indexof['one','two','three'], 'one'); -- 1 631 select indexof[1, 2, 4], 4); -- 3 632 select 633 indexof['one','two','three'], 'one'), -- 1 634 indexof['one',null,null], null),-- 1返回第一个找到的元素的索引位置 635 indexof[1, 2, 4], 4); -- 3 636 -- 数组元素的以第一个和最后一个元素。 637 select length[12,3,4,4,4]); 638 select array12,22,31)[1]; 639 640 with 641 [23,43,565,2,32,34] as arr 642 select 643 arr[1], -- 去除数组中的第一个元素 644 arr[lengtharr)]; -- 提取元素中的最后一个元素 645 646 -- 10.计算数组中包含指定元素的个数 647 -- countequalarr, x) 返回数组中等于x的元素的个数。相当于arraycount(elem - > elem = x,arr)。 648 -- 注意:null值将作为单独的元素值处理。 649 select 650 countequal[1, 2, 2, 2, 3, 4], 2), -- 3 651 countequal[1, 2, null, null], null); -- 2 652 653 -- 11.arrayenumeratearr) 返回 array [1, 2, 3, ..., length arr) ] 此功能通常与array join一起使用。它允许在应用array join后为每个数组计算一次。 654 select arrayenumerate[1,20,20,3]); -- [1,2,3,4] 655 select arrayenumeratearray11,20,13)); -- [1,2,3] 656 select arrayenumeratearray11,20,13,null)); -- [1,2,3,4] 注意:null也算是一个元素。 657 --arrayenumerateuniqarr) 返回与源数组大小相同的数组,其中每个元素表示与其下标对应的源数组元素在源数组中出现的次数 658 select arrayenumerateuniq[1,1,2,2]); -- [1,2] 659 660 -- 12.删除数组的元素 661 -- arraypopbackarray) 删除数组array的最后一项 662 select arraypopbackarray1,2,3,0)) as res; -- [1,2,3] 663 -- arraypopfrontarray) 从数组中删除第一项 664 select arraypopfrontarray0,1,2,3)) as res; -- [1,2,3] 665 666 -- 13.添加数组的元素 arraypushfrontarray, single_value) single_value是单个值 667 select arraypushback[1,2,3], 0) as res; -- [1,2,3,0] 668 select arraypushfront[1,2,3], 0) as res; -- [0,1,2,3] 669 670 -- 14.更改数组的长度 arrayresizearr, size[, extender]) 671 -- 如果arr的长度 > size,则会对arr截取size的长度; 672 -- 如果arr的长度 < size,则其余位置用对应数据类型的默认值填充。 673 -- 注意:extender含义是扩展元素的值。如果没有指定extender,则默认按照对应的数据类型的默认值进行赋值。否则按照extender进行填充。 674 select arrayresize[1,2,3], 5); -- [1,2,3,0,0] 675 select arrayresize[1,2,3], 2); -- [1,2] 676 select arrayresize[1,2,3], 3); -- [1,2,3] 677 --↓↓↓ runtimeexception: parse exception: bytefragment{[[[1,2],[3,4],[5,6],[],[]]], start=0, len=25} 678 select arrayresize[array1,2),array3,4),array5,6)], 5); 679 select arrayresize[1,2,3], 5, 12); -- [1,2,3,12,12] 680 select arrayresize['one','two','three'], 5); -- ['one','two','three','',''] 681 select arrayresize['one','two','three'], 5, 'default'); -- ['one','two','three','default','default'] 682 683 -- 15.截取数组的部分元素,得到一个新的子数组 684 -- arrayslicearray, offset[, length]) 685 -- 解释: 686 -- array: 数组, 687 -- offset – 数组的偏移。正值表示左侧的偏移量,负值表示右侧的缩进值。数组下标从1开始。 688 -- length - 子数组的长度。如果指定负值,则该函数返回[offset,array_length - length。如果省略该值,则该函数返回[offset,the_end_of_array]。 689 select 690 arrayslice[1,2,3,4,5,6], 0, 3), -- 无返回值 691 arrayslice[1,2,null,5,6], 1, 3), -- [1,2,0] 692 arrayslice['one','two',null], 1, 3), -- ['one','two',''] 693 arrayslice[1,2,3,4,5,6], 1, 3); -- [1,2,3] 694 695 -- 16.数组排序:arraysort[func,] arr, ……) 696 -- 注意:如果在字符串数组中,''和null是需要特别对待的,''需要放在最前面,而null则是按顺序存放到最后的。 697 -- arraysort是高阶函数。您可以将lambda函数作为第一个参数传递给它。在这种情况下,排序顺序由lambda函数的调用结果决定。 698 select 699 arraysort['a','',null,'c','b']) as hasnullempty1, --['','a','b','c',''] 第一个是'',最后一个''起始是null) 700 arraysortarray'ac','ab','bc','ad',null)) as hasnull, -- ['ab','ac','ad','bc',''] 701 arraysortarray'ac','','ab',null,'bc','ad',null)) as hasnullempty2, -- ['','ab','ac','ad','bc','',''] 702 arraysort[5,4,3,2,1]) as numsorted,-- [1,2,3,4,5] 数字排序) 703 arraysort['ca','bb','ac']) as strsorted;-- ['ac','bb','ca'] 字符串排序) 704 select 705 arraysort[null, 1, 3, null, 2]) as sortedarr, -- [1,2,3,0,0] 706 arrayreversesortedarr) as reversesortdarr;-- [0,0,3,2,1] 707 -- 下面这种排序的实质,正数转成负数,再在数学上比较升序排序。 708 select arraysortx -> -x, [1,2,3]) as res; -- [3,2,1] 降序:(高阶函数用法) 709 select arraysortx) -> -x, [1,2,3]) as res; -- [3,2,1] 降序:(高阶函数用法) 710 select arraysortx -> x, [5,4,3,1,2,3]) as res; -- [1,2,3,3,4,5] 升序:(高阶函数用法) 711 select arraysortx) -> x, [5,4,3,1,2,3]) as res; -- [1,2,3,3,4,5] 升序:(高阶函数用法) 712 -- arraysortlambda, arr1, arr2) 713 select arraysortx, y) -> y, ['hello', 'world'], [2, 1]) as res; -- ['world','hello'] 714 select arraysortx, y) -> -y, [0, 1, 2], [1, 2, 3]) as res; -- [2,1,0] 715 -- 再次提醒:null, nan, inf的排序顺序: 716 -- 含义: 717 -- -inf 是数组中的第一个。 718 -- null 是数组中的最后一个。 719 -- nan 在null的前面。 720 -- inf 在nan的前面。 721 -- 出现异常:runtimeexception: parse exception: 722 -- bytefragment{[[-inf,-4,1,2,3,inf,nan,nan,null,null]], start=0, len=37} 723 select arraysort[1, nan, 2, null, 3, nan, -4, null, inf, -inf]); 724 725 -- 17.数组翻转:arrayreverse[func,] arr, ……) 726 -- 如果是null的话在排序的过程中,根据数组的数据类型进行默认值填充。 727 select 728 arrayreversearray'a','b','c',null)) as hasonenull, -- ['','c','b','a'] 729 arrayreversearray'ac','ab','bc','ad',null)) as hasnull, -- ['','ad','bc','ab','ac'] 730 --网格视图: ['[null]','ad','bc','','ab','[null]','','ac'];文本视图 :['','ad','bc','','ab','','','ac'] 731 arrayreversearray'ac','',null,'ab','','bc','ad',null)) as hasnullempty, 732 arrayreversearraynull, 3, null, 2, 1)),-- [1,2,0,3,0] 733 arrayreverse[1,2,3,4]);-- [4,3,2,1] 734 735 -- 18.数组排序并翻转:arraysort[func,] arr, ...) 736 select arrayreversesort[1, 3, 3, 0]); -- [3,3,1,0] 737 select arrayreversesort['hello', 'world', '!']); -- ['world','hello','!'] 738 --runtimeexception: parse exception: bytefragment{[[inf,3,2,1,-4,-inf,nan,nan,null,null]], start=0, len=37} 739 select arrayreversesort[1, nan, 2, null, 3, nan, -4, null, inf, -inf]) as res;-- [inf,3,2,1,-4,-inf,nan,nan,null,null] 740 -- 下面的执行顺序为: 741 -- 1.首先,根据lambda函数的调用结果对源数组([1, 2, 3])进行排序。 结果是[3, 2, 1]。 742 -- 2.反转上一步获得的数组。 所以,最终的结果是[1, 2, 3]。 743 select arrayreversesortx) -> -x, [1, 2, 3]) as res; -- [1,2,3] 744 select arrayreversesortx) -> x, [1, 2, 3]) as res; -- [1,2,3] 745 -- 下面的执行顺序为: 746 -- 1.首先,根据lambda函数的调用结果对源数组(['hello','world'])进行排序。 其中,在第二个数组([2,1])中定义了源数组中相应元素的排序键。 所以,排序结果['world','hello']。 747 -- 2.反转上一步骤中获得的排序数组。 所以,最终的结果是['hello','world']。 748 select arrayreversesortx, y) -> y, ['hello', 'world'], [2, 1]) as res;-- ['hello','world'] 749 select arrayreversesortx, y) -> -y, ['hello', 'world'], [2, 1]) as res;-- ['world','hello'] 750 select arrayreversesortx, y) -> x, ['hello', 'world'], [2, 1]) as res;-- ['world','hello'] 751 --出现异常:illegal type string of argument 752 --select arrayreversesortx, y) -> -x, ['hello', 'world'], [2, 1]) as res; 753 select arrayreversesortx, y) -> x, ['hello', 'world'], [1, 2]) as res;-- ['world','hello'] 754 755 -- 19.统计数组中不重复元素的个数。arrayuniqarr,……) 756 -- ① 如果传递一个参数,则计算数组中不同元素的数量。 757 -- ② 如果传递了多个参数,则它计算多个数组中相应位置的不同元素元组的数量 758 select 759 arrayuniq[1,2,3]), -- 3 760 arrayuniq[1,2,2,2,3]); -- 3 761 select 762 arrayuniq[1,2,3],[2,3,4]), 763 arrayuniq[1,2,2],[1,3,3]); 764 765 -- 20.数组的特殊功能:arrayjoinarr) 这是一个非常有用的函数。 766 -- 解释:此函数将数组作为参数,并将该行在结果集中复制数组元素个数 767 select arrayjoin[1, 2, 3] as src) as dst, 'hello', src; 768 -- 每个元素扩大两倍; 769 select arrayjoin[1,2,3]) * 2; 770 select arrayjoin[-1,-2,0,1,2]) * 2; 771 --出现异常: illegal types arrayuint8) and arrayuint8) of arguments of function multiply 772 --select multiplyarray1,2,3), 2); 773 select multiplyarrayjoin[-1,-2,0,1,2]), 2); 774 -- 每个元素缩小两倍 775 select arrayjoin[-4,-2,0,2,4]) / 2; 776 select dividearrayjoin[-4,-2,0,2,4]) , 2); 777 778 779 -- 21.arraydifferencearr) 780 -- 返回一个数组,其中包含所有相邻元素对之间的差值 781 select arraydifference[1,2,3,4]);-- [0,1,1,1] 782 select arraydifference[1,3,10,50]);-- [0,2,7,40] 783 784 785 -- 22. arraydistinctarr)返回一个包含所有数组中不同元素的数组. 786 -- 类似于java的set集合,对list集合进行去重。 787 select arraydistinctarray1,2,3,4,4,4)); -- [1,2,3,4] 788 select arraydistinct[1,2,2,3,4,2,2,5,4,5]); -- [1,2,3,4,5] 789 select arraydistinctarray0,1,null,3,4,4,4)); -- [0,1,3,4] 790 -- 数组去重统计元素个数 791 select uniqarrayjoin[1,2,3,6,3])); -- 4 表示数组去重后元素的个数 792 select uniqarray[1,2,3,4,1,2,3,4]); -- 4 表示数组去重后元素的个数 793 -- 数组元素累计 794 select sumarray[1,2,3,4,5]);-- 15 795 select sumarraysum[1,2,3,4,5])); -- 15 796 797 798 -- 23. arrayenumeratedensearr) 返回与源数组大小相同的数组,指示每个元素首次出现在源数组中的位置 799 select 800 arrayenumeratedense[10,20,20,10,30]) as numarrenumdense,-- [1,2,2,1,3] 801 -- [1,1,2,3,4,1,3,5,5] 802 arrayenumeratedense[10,10,2,12,3,10,12,null,null]) as arrenumdensehasnull, 803 -- [1,2,1,1,2,3] 804 arrayenumeratedense[10,20,10,10,20,30]) as arrenumdese2; 805 806 -- 24. arrayintersectarr,……) 返回所有数组元素的交集。 807 -- 如果arr的数目只有一个,则返回它本身;如果有多个数组,则返回所有数组中元素的交集。 808 select 809 -- 注意:最后得到的数组元素的顺序。(有什么影响吗?) 810 arrayintersect['one','two'],['one','two','three']) as unistrarr1, -- ['two','one'] 811 arrayintersect['aaa','bbb'],['bbb','aaa','three']) as unistrarr2, -- ['bbb','aaa'] 812 arrayintersect[1,2],[1,2,3]) as uniarr1, -- [1,2] 813 arrayintersect[1,2],[1,2,3],[2,3,4],[2,3,4]) as uniarr2; -- 2 814 select 815 arrayintersect[1,2], [3,4]), -- [] 816 arrayintersect[1,2]);-- [1,2] 817 818 -- 25.arrayreduceagg_func, arr1, ...) 819 -- agg_func 为聚合函数,传入到数组当中。 820 -- 将聚合函数应用于数组并返回其结果.如果聚合函数具有多个参数,则此函数可应用于相同大小的多个数组。 821 select 822 arrayreduce'max', [1,2,3]) as minnum,--最大值 3 823 arrayreduce'min', [1,2,3]) as maxnum,--最小值 1 824 arrayreduce'sum', [1,2,3]) as sumnum;--求和 6 825 826 -- 十三、 字符串查分合并函数 827 --->>>>>> 字符串拆分合并函数 828 -- 1.splitbycharseparator, s) 将字符串以‘separator’拆分成多个子串。 829 -- ‘separator’必须为仅包含一个字符的字符串常量。 返回拆分后的子串的数组。 830 -- 如果分隔符出现在字符串的开头或结尾,或者如果有多个连续的分隔符,则将在对应位置填充空的子串。 831 select splitbychar',', 'hello,world!'); -- ['hello','world!'] 832 --下面异常:illegal separator for function splitbychar. must be exactly one byte. 833 --select splitbychar'or', 'hello,world!'); 834 835 -- 2.splitbystringseparator, s) 836 -- 与上面相同,但它使用多个字符的字符串作为分隔符。 该字符串必须为非空 837 select splitbystring'or','goodorniceorgreat'); -- ['good','nice','great'] 838 839 -- 3.alphatokenss) 从范围a-z和a-z中选择连续字节的子字符串。返回子字符串数组 840 select alphatokens'abca1abc'); -- ['abca','abc'] 841 select alphatokens'abc1232abc2wer3rtty'); -- ['abc','abc','wer','rtty'] 842 843 -- 4.数组元素合并函数:arraystringconcatarr[, sparator]) 844 -- 使用separator将数组中列出的字符串拼接起来。 845 -- ‘separator’是一个可选参数:一个常量字符串,默认情况下设置为空字符串。 返回拼接后的字符串 846 select arraystringconcat[1,2,3], '-'); -- 出现异常,要求数组必须是字符串string类型的元素 847 select arraystringconcat['one','two','three']); -- onetwothree 848 select arraystringconcat['one','two','three'], '-'); -- one-two-three 849 select arraystringconcat['one','two','three',''], '-');-- one-two-three- 注意:null不能存在arr中 850 851 852 --十四、位操作符 853 --->>>>>> 位操作符 854 --位操作函数适用于uint8,uint16,uint32,uint64,int8,int16,int32,int64,float32或float64中的任何类型。 855 --结果类型是一个整数,其位数等于其参数的最大位。 856 --如果至少有一个参数为有符数字,则结果为有符数字。如果参数是浮点数,则将其强制转换为int64。 857 select 858 bitand1,0), -- 0 859 bitand1,1), -- 1 860 bitand1,2), -- 0 861 bitand-1,0), -- 0 862 bitand-1,-2), -- -2 863 bitand-10,-1), -- -10 864 bitor1,2), -- 3 865 bitor1,0), -- 1 866 bitor2,0), -- 2 867 bitor0,2); -- 2 868 select bitxor1, 2), bitxor20, 15), bitnot2);-- 3 27 253 869 870 871 --十五、hash函数:可以用于将元素不可逆的伪随机打乱。 872 -- 注意:伪随机! 873 select 874 -- 计算字符串的md5值。( 如果您不需要一定使用md5,请使用‘siphash64’函数。) 875 halfmd5'hello world!'), 876 halfmd512); 877 select 878 md5'drew-zero,78967'); 879 880 select 881 -- 为任何类型的整数计算32位的哈希。 这是相对高效的非加密hash函数 882 inthash321221232132132) as inthash32, 883 -- 推荐:从任何类型的整数计算64位哈希码。 它的工作速度比inthash32函数快。 884 inthash641221232132132) as inthash64, 885 -- 计算任意数量字符串的cityhash64或使用特定实现的hash函数计算任意数量其他类型的hash。 886 cityhash64'username') as cityhash64, 887 -- 1.使用sha1或者sha224加密的话,只能用于字符串 888 -- 2.字符串 需使用单引号。 889 sha1'1232131') as sha1, 890 sha224'1232131') as sha224, 891 sha256'drew-zero') as sha256; 892 893 -- urlhashurl[, n]) 一种快速的非加密哈希函数,用于规范化的从url获得的字符串 894 -- 从一个字符串计算一个哈希,如果结尾存在尾随符号/,?或#则忽略。 urlhash(s,n) 895 -- 计算url层次结构中字符串到n级别的哈希值,如果末尾存在尾随符号/,?或#则忽略。 url的层级与urlhierarchy中的层级相同 896 -- 用处:此函数被用于yandex.metrica。 897 select 898 urlhash'www.baidu.com'), -- 11390370829909720855 899 urlhash'www.baidu.com', 0), -- 11390370829909720855 900 -- 901 urlhash'www.baidu.com', 1); -- 11160318154034397263 902 903 -- farmhash64s) 计算字符串的farmhash64。 接受一个string类型的参数。返回uint64。 904 select farmhash64'www.runoob.com'); -- 6668483584160323388 905 906 -- javahashs) 计算字符串的javahash。 接受一个string类型的参数。返回int32。 907 select javahash'www.baidu.com'); -- 270263191 908 909 -- hivehashs) 计算字符串的hivehash。 接受一个string类型的参数。返回int32。 与javahash相同,但不会返回负数 910 select hivehash'www.baidu.com'); -- 270263191 911 912 913 --十六、随机函数 914 --->>>>>> 随机函数 915 -- 解释:随机函数使用非加密方式生成【伪随机】数字。 916 -- ① 所有随机函数都只接受一个参数或不接受任何参数。 917 -- ② 您可以向它传递任何类型的参数,但传递的参数将不会使用在任何随机数生成过程中。 918 -- ③ 此参数的唯一目的是防止公共子表达式消除,以便在相同的查询中使用相同的随机函数生成不同的随机数 919 -- rand) 函数:返回一个uint32类型的随机数字,所有uint32类型的数字被生成的概率均相等。 920 -- rand64) 函数:返回一个uint64类型的随机数字,所有uint64类型的数字被生成的概率均相等。 921 -- randconstant) 函数:返回一个uint32类型的随机数字,该函数不同之处在于仅为每个数据块参数一个随机数。 922 select 923 rand), -- 1751687411 924 rand10), -- 1124981728 925 rand64), 926 rand6410), 927 randconstant), 928 randconstant); 929 930 931 -- 十七、编码函数: 932 -- hex), unhex), uuidstringtonumstr), uuidnumtostringstr),bitmasktolistnum) ... 933 -- 1.hex函数编码 934 select 935 -- 68656c6c6f20776f726c64212c68656c6c6f20636c69636b686f757365 936 hex'hello world!,hello clickhouse') as hexstr, 937 hexnow)) as hexdatetime, -- 5d414ba2 938 hextodatenow))) as hexdate; --46bc 939 940 -- 2.接受包含36个字符的字符串,格式为“123e4567-e89b-12d3-a456-426655440000”,并将其转化为fixedstring(16)返回 941 select uuidstringtonum'123e4567-e89b-12d3-a456-426655440000'); 942 943 -- 3. 接受一个整数。返回一个uint64类型数组,其中包含一组2的幂列表,其列表中的所有值相加等于这个整数。数组中的数字按升序排列。 944 -- bitmasktoarraynum) 945 select bitmasktoarray10); -- [2,8] 946 select bitmasktoarray100); -- [4,32,64] 947 948 -- 4.接受一个整数。返回一个字符串,其中包含一组2的幂列表,其列表中的所有值相加等于这个整数。列表使用逗号分割,按升序排列。 949 -- bitmasktolistnum) 950 select bitmasktolist10); -- 2,8 951 select bitmasktolist100); -- 4,32,64 952 select bitmasktolist0); -- '' 空字符串 953 954 955 --十八、uuid函数 956 --->>>>>> uuid函数 957 -- 1.generateuuidv4) 返回 uuid类型的值。 958 select generateuuidv4) as randomuuid; -- 随机生成一个uuidv4的字符串(b6940dfe-0dc9-4788-bac7-319d13235a2e) 959 select replacealltostringgenerateuuidv4)), '-', '') as replaceuuid; -- 9d1947ea4fcf450da5391feb6142cab6 960 961 -- 2.touuids) 将string类型的值 转换成uuid类型的值 962 select touuid'61f0c404-5cb3-11e7-907b-a6006ad3dba0') as uuid; 963 964 -- 3.接受一个string类型的值,其中包含36个字符且格式为xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, 965 -- 将其转换为uuid的数值并以fixedstring16)将其返回。 966 select 967 '612f3c40-5d3b-217e-707b-6a546a3d7b29' as uuid, -- 612f3c40-5d3b-217e-707b-6a546a3d7b29 968 uuidstringtonumuuid) as bytes; --a/<@];!~p{jtj={) 969 970 -- 4. uuidnumtostring) 接受一个fixedstring16)类型的值,返回其对应的string表现形式。 971 select 'a/<@];!~p{jtj={)' as bytes, 972 uuidnumtostringtofixedstringbytes, 16)) as uuid; 973 974 --- 二十、 url函数:所有这些功能都不遵循rfc。它们被最大程度简化以提高性能。 975 --- 什么事rfc? 976 ---- request for comments(rfc),是一系列以编号排定的文件。文件收集了有关互联网相关信息,以及unix和互联网社区的软件文件。 977 -- 1. 截取函数:如果url中没有要截取的内容则返回空字符串。 978 select protocol'http://www.baidu.com');-- http 979 select protocol'https://www.baidu.com');-- https 980 select protocol'www.baidu.com');-- '' 981 -- 获取域名。 982 select domain'http://www.baidu.com'); -- www.baidu.com 983 select domain'https://www.google.com.cn'); -- www.google.com.cn 984 -- 返回域名并删除第一个‘www.’ 985 select domainwithoutwww'http://www.baidu.com');-- baidu.com 986 select domainwithoutwww'www.baidu.com');-- '' 987 -- 返回顶级域名。例如:.ru 988 select topleveldomain'http://www.runoob.com.cn'); -- cn 989 select topleveldomain'https://www.huse.edn'); -- edu 990 -- 返回“第一个有效子域名” 991 -- 如果顶级域名为‘com’,‘net’,‘org’或者‘co’则第一个有效子域名为二级域名。否则则返回三级域名 992 select firstsignificantsubdomain'https://news.yandex.com.tr/'); -- yandex 993 -- 返回包含顶级域名与第一个有效子域名之间的内容参阅上面内容) 994 select cuttofirstsignificantsubdomain'https://news.yandex.com.tr/'); -- yandex.com.tr 995 -- 返回url路径 996 select path'https://blog.csdn.net/u012111465/article/details/85250030');-- /u012111465/article/details/85250030 997 -- 与上面相同,但包括请求参数和fragment。 998 select pathfull'https://clickhouse.yandex/#quick-start'); -- /#quick-start 999 -- 返回请求参数。例如:page=1&lr=213。请求参数不包含问号已经# 以及# 之后所有的内容。 1000 select querystring'http://www.baidu.com/?page=1&lr=234'); -- page=1&lr=234 根据?确定) 1001 select querystring'http://www.baidu.com/page=1&lr=234'); -- '' 1002 -- 返回url的fragment标识。fragment不包含#。 1003 select fragment'https://clickhouse.yandex/#quick-start'); -- quick-start 1004 -- 返回请求参数和fragment标识。例如:page=1#29390。 1005 select querystringandfragment'https://www.baidu.com/s?ie=utf-8&rsv_sug7=100#ei-ai'); -- ie=utf-8&rsv_sug7=100#ei-ai 1006 1007 1008 -- 2. 删除url中的部分内容 如果url中不包含指定的部分,则url不变。) 1009 select cutwww'www.baidu.com');-- www.baidu.com 1010 select cutwww'https://www.baidu.com');-- www.baidu.com 1011 select cutwww'https://www.baidu.com');-- www.baidu.com 1012 -- 删除请求参数 1013 select cutquerystring'http://www.baidu.com/1?page=1'); -- http://www.baidu.com/1 1014 -- 删除fragment标识。#同样也会被删除。 1015 select cutfragment'http://www.baidu.com/#quick-demo'); -- http://www.baidu.com/ 1016 -- 删除请求参数以及fragment标识。问号以及#也会被删除。 1017 select cutquerystringandfragment'http://www.baidu.com/1?page=23#we'); -- http://www.baidu.com/1 1018 -- cuturlparameterurl, name) 删除url中名称为‘name’的参数。下面例子中的参数是:&之后,resv,name 1019 select cuturlparameter'http://www.baidu.com/1?page=1#erre&resv=23&name=user','resv'); 1020 1021 1022 --二十一、ip函数 1023 1024 1025 1026 --二十二、条件函数 1027 select if12 > 10 , 12, 20); 1028 select 12 > 10 ? 12 : 10; 1029 select ifgreater12, 10), 12, 10); 1030 1031 1032 1033 --二十三、操作符函数替换 1034 -- clickhouse自带的计算操作符函数(对接mybatis的时候不用将“<”之类的符号转换成 “age1 ”) 1035 -- 1.等于注意函数名称的大小,严格区分大小写) 1036 select 1037 equals'hello','hello'), -- 1 1038 equals'ab','ba'); -- 0 1039 -- 2.不等于 1040 select 1041 notequals'a','b'), -- 1 1042 notequals'a','a'), -- 0 1043 notequals12, 12), -- 1 1044 notequals12, 1010); -- 0 1045 -- 3.大于 如果前者大于后者,则返回1;否则返回0) 1046 select 1047 greater12, 10), -- 1 1048 greater10, 12), -- 0 1049 greater12, 12), -- 0 1050 greater'b','a'), -- 1 1051 greater'a','b'); -- 0 1052 -- 3.1 扩展:提取两者中最大的值 1053 select greatest12,11); -- 12 1054 -- 4.小于(如果前者小于后者,则返回1;否则返回0) 1055 select less12,23); -- 1 1056 select less120,23); -- 0 1057 -- 5.大于或等于 1058 select greaterorequals12,12); -- 1 1059 select greaterorequals120,12); -- 1 1060 -- 6.小于或等于 1061 select lessorequals12,12); -- 1 1062 select lessorequals12,129); -- 1 1063 -- ===== string操作 1064 -- *. a like s 1065 select like'a', 'abcd'); -- 0 1066 select like'a', 'a'); -- 1
以上就是小编对于clickhouse日期函数问题和相关问题的解答了,clickhouse日期函数的问题希望对你有用!
© ag真人官方网址的版权声明
本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如有侵权请联系网站管理员删除,联系邮箱1856753@qq.com。
the end