常用函数汇总
[日期]
注:所有的时间日期函数都可以在第二个可选参数中接受时区参数。示例:Asia / Yekaterinburg。在这种情况下,它们使用指定的时区而不是本地(默认)时区。
now() // 2022-08-08 17:19:00 取当前时间 toYear() // 2022 取日志中的年份 toMonth() // 8 取日子中的月份 today() // 2022-08-08 今天的日期 yesterday() // 2022-08-07 昨天的日期 toDayOfYear() // 100 取一年中的第几天 toDayOfWeek() // 1 取一周中的第几天 toHour() 17 取小时 toMinute() 22 取分钟 toSecond() 33 取秒 toStartOfYear() 2022-01-01 取一年中的第一天 toStartOfMonth() 2022-08-01 取当月的第一天 toStartOfQuarter(time), formatDatetime(now(), '%Y-%m-%d') // 2022-08-08 指定时间格式 toYYYYMM() //202004 toYYYYMMDD() //20200401 toYYYYMMDDhhmmss() //20200401172540 toUnixTimestamp(time) 将datetime格式转换成Unix时间戳 toQuarter(time) 季度(一年分为四个季度。1(一季度:1-3),2(二季度:4-6),3(三季度:7-9),4(四季度:10-12))
未来日期:
-- 第一种,日期格式(指定日期,需注意时区的问题) WITH toDate('2019-09-09') AS date, toDateTime('2019-09-09 00:00:00') AS date_time SELECT addYears(date, 1) AS add_years_with_date, addYears(date_time, 0) AS add_years_with_date_time; -- 第二种,日期格式(当前,本地时间) WITH toDate(now()) as date, toDateTime(now()) as date_time SELECT now() as now_time,-- 当前时间 -- 之后1年 addYears(date, 1) AS add_years_with_date, addYears(date_time, 1) AS add_years_with_date_time, -- 之后1月 addMonths(date, 1) AS add_months_with_date, addMonths(date_time, 1) AS add_months_with_date_time, --之后1周 addWeeks(date, 1) AS add_weeks_with_date, addWeeks(date_time, 1) AS add_weeks_with_date_time, -- 之后1天 addDays(date, 1) AS add_days_with_date, addDays(date_time, 1) AS add_days_with_date_time, --之后1小时 addHours(date_time, 1) AS add_hours_with_date_time, --之后1分中 addMinutes(date_time, 1) AS add_minutes_with_date_time, -- 之后10秒钟 addSeconds(date_time, 10) AS add_seconds_with_date_time, -- 之后1个季度 addQuarters(date, 1) AS add_quarters_with_date, addQuarters(date_time, 1) AS add_quarters_with_date_time;
过去日期:
WITH toDate(now()) as date, toDateTime(now()) as date_time SELECT subtractYears(date, 1) AS subtract_years_with_date, subtractYears(date_time, 1) AS subtract_years_with_date_time, subtractQuarters(date, 1) AS subtract_Quarters_with_date, subtractQuarters(date_time, 1) AS subtract_Quarters_with_date_time, subtractMonths(date, 1) AS subtract_Months_with_date, subtractMonths(date_time, 1) AS subtract_Months_with_date_time, subtractWeeks(date, 1) AS subtract_Weeks_with_date, subtractWeeks(date_time, 1) AS subtract_Weeks_with_date_time, subtractDays(date, 1) AS subtract_Days_with_date, subtractDays(date_time, 1) AS subtract_Days_with_date_time, subtractHours(date_time, 1) AS subtract_Hours_with_date_time, subtractMinutes(date_time, 1) AS subtract_Minutes_with_date_time, subtractSeconds(date_time, 1) AS subtract_Seconds_with_date_time;
计算时间差值:
-- 第一种:指定时间计算差值示例 WITH toDateTime('2019-07-30 10:10:10', 'Asia/Shanghai') as date_shanghai_one, toDateTime('2020-10-31 11:20:30', 'Asia/Shanghai') as date_shanghai_two SELECT dateDiff('year', date_shanghai_one, date_shanghai_two) as diff_years, dateDiff('month', date_shanghai_one, date_shanghai_two) as diff_months, dateDiff('week', date_shanghai_one, date_shanghai_two) as diff_week, dateDiff('day', date_shanghai_one, date_shanghai_two) as diff_days, dateDiff('hour', date_shanghai_one, date_shanghai_two) as diff_hours, dateDiff('minute', date_shanghai_one, date_shanghai_two) as diff_minutes, dateDiff('second', date_shanghai_one, date_shanghai_two) as diff_seconds; -- 第二种:本地当前时间示例 WITH now() as date_time SELECT dateDiff('year', date_time, addYears(date_time, 1)) as diff_years, dateDiff('month', date_time, addMonths(date_time, 2)) as diff_months, dateDiff('week', date_time, addWeeks(date_time, 3)) as diff_week, dateDiff('day', date_time, addDays(date_time, 3)) as diff_days, dateDiff('hour', date_time, addHours(date_time, 3)) as diff_hours, dateDiff('minute', date_time, addMinutes(date_time, 30)) as diff_minutes, dateDiff('second', date_time, addSeconds(date_time, 35)) as diff_seconds;
聚合函数组合器
聚合函数的名称可以附加一个后缀。 这改变了聚合函数的工作方式。
-
If
-
Array
-
ForEach
-
OrDefault
-
OrNull
-
Resample
-
State
-
Merge
-
MergeState
2. 创建测试表
sql
-- 创建雇员表
CREATE TABLE emp(
`empno` UInt16 NOT NULL COMMENT '员工编码',
`ename` String COMMENT '员工姓名',
`job` String COMMENT '职位',
`mgr_no` UInt16 COMMENT '领导的员工编号',
`hiredate` Date COMMENT '入职日期',
`sal` decimal(7, 2) COMMENT '月薪',
`comm` decimal(7, 2) COMMENT '奖金',
`deptno` UInt8 COMMENT '所属部门编号'
)
ENGINE = MergeTree()
order by empno;
-- 向雇员表写入数据
insert into emp (empno, ename, job, mgr_no, hiredate, sal, comm, deptno) values
(7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, null, 20),
(7499, 'ALLEN', 'SALESMAN', 7698,'1981-02-20', 1600, 300, 30),
(7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30),
(7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, null, 20),
(7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30),
(7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, null, 30),
(7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, null, 10),
(7788, 'SCOTT', 'ANALYST', 7566, '1987-04-19', 3000, null, 20),
(7839, 'KING', 'PRESIDENT', null, '1981-11-17', 5000, null, 10),
(7844, 'TURNER'