1. 程式人生 > 其它 >五、clickhouse時間日期函式

五、clickhouse時間日期函式

常用方式:

SELECT

toDateTime('2019-07-30 10:10:10') AS time,
-- 將DateTime轉換成Unix時間戳
toUnixTimestamp(time) as unixTimestamp,
-- 保留 時-分-秒
toDate(time) as date_local,
toTime(time) as date_time,-- 將DateTime中的日期轉換為一個固定的日期,同時保留時間部分。
-- 獲取年份,月份,季度,小時,分鐘,秒鐘
toYear(time) as get_year,
toMonth(time) as get_month,
-- 一年分為四個季度。1(一季度:1-3),2(二季度:4-6),3(三季度:7-9),4(四季度:10-12)
toQuarter(time) as get_quarter,
toHour(time) as get_hour,
toMinute(time) as get_minute,
toSecond(time) as get_second,
-- 獲取 DateTime中的當前日期是當前年份的第幾天,當前月份的第幾日,當前星期的周幾
toDayOfYear(time) as "當前年份中的第幾天",
toDayOfMonth(time) as "當前月份的第幾天",
toDayOfWeek(time) as "星期",
toDate(time, 'Asia/Shanghai') AS date_shanghai,
toDateTime(time, 'Asia/Shanghai') AS time_shanghai,
-- 得到當前年份的第一天,當前月份的第一天,當前季度的第一天,當前日期的開始時刻
toStartOfYear(time),
toStartOfMonth(time),
toStartOfQuarter(time),
toStartOfDay(time) AS cur_start_daytime,
toStartOfHour(time) as cur_start_hour,
toStartOfMinute(time) AS cur_start_minute,
-- 從過去的某個固定的時間開始,以此得到當前指定的日期的編號
toRelativeYearNum(time),
toRelativeQuarterNum(time);

SELECT
toDateTime('2019-07-30 14:27:30') as time,
toISOYear(time) AS iso_year,
toISOWeek(time) AS iso_week,
now() AS cur_dateTime1, -- 返回當前時間yyyy-MM-dd HH:mm:ss
today() AS cur_dateTime2, -- 其功能與'toDate(now())'相同
yesterday() AS yesterday, -- 當前日期的上一天
-- timeSlot(1) AS timeSlot_1, -- 出現異常!!將時間向前取整半小時
toDate(time) as getY_M_d;

-- 目前只有這三種格式,沒有什麼toYYYY(),toYYYddmm()之類的函式,不要想當然。
SELECT
now() as nowTime,
-- 將Date或DateTime轉換為包含年份和月份編號的UInt32型別的數字(YYYY * 100 + MM)
toYYYYMMDDhhmmss(nowTime),
toYYYYMMDD(nowTime),
toYYYYMM(nowTime);

-- formatDateTime(Time, Format[,Timezone])函式引用
SELECT
now() as now_time,
toDateTime('2019-07-31 18:20:30') AS def_datetime,
formatDateTime(now_time, '%D') AS now_time_day_month_year,-- 07/30/19
-- toDateTime('2019-07-31 18:20:30', 'Asia/Shanghai') AS def_datetime1, -- 指定時區
formatDateTime(def_datetime, '%Y') AS def_datetime_year, -- 2019(指定日期為2019年)
formatDateTime(def_datetime, '%y') AS def_datetime_year_litter, -- 19(指定日期為19年,Year, last two digits (00-99),本世紀的第19年)
formatDateTime(def_datetime, '%H') AS hour24, -- 18 下午六點
formatDateTime(def_datetime, '%I') AS hour12, -- 06下午六點
formatDateTime(def_datetime, '%p') AS PMorAM, -- 指定時間是上午還是下午
formatDateTime(def_datetime, '%w') AS def_datetime_get_curWeek,-- 3(指定日期為星期三)
formatDateTime(def_datetime, '%F') AS def_datetime_get_date,-- 2019-07-31
formatDateTime(def_datetime, '%T') AS def_datetime_get_time,-- 18:20:30
formatDateTime(def_datetime, '%M') AS def_datetime_get_minute,-- 20(得到指定事件的“分”,minute (00-59))
formatDateTime(def_datetime, '%S') AS def_datetime_get_second;-- 30(得到指定事件的“秒”,second (00-59))

-- 1.跳轉到之後的日期函式
-- 第一種,日期格式(指定日期,需注意時區的問題)
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,-- 當前時間
addYears(date, 1) AS add_years_with_date,-- 之後1年
addYears(date_time, 1) AS add_years_with_date_time,
addMonths(date, 1) AS add_months_with_date,-- 之後1月
addMonths(date_time, 1) AS add_months_with_date_time,
addWeeks(date, 1) AS add_weeks_with_date,--之後1周
addWeeks(date_time, 1) AS add_weeks_with_date_time,
addDays(date, 1) AS add_days_with_date,-- 之後1天
addDays(date_time, 1) AS add_days_with_date_time,
addHours(date_time, 1) AS add_hours_with_date_time,--之後1小時
addMinutes(date_time, 1) AS add_minutes_with_date_time,--之後1分中
addSeconds(date_time, 10) AS add_seconds_with_date_time,-- 之後10秒鐘
addQuarters(date, 1) AS add_quarters_with_date, -- 之後1個季度
addQuarters(date_time, 1) AS add_quarters_with_date_time;

-- 2.跳轉到當前日期之前的函式(函式將Date/DateTime減去一段時間間隔,然後返回Date/DateTime)


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;

SELECT toDate('2019-07-31', 'Asia/GuangZhou') as date_guangzhou;
SELECT toDate('2019-07-31'), toDate('2019-07-31', 'Asia/Beijing') as date_beijing;
-- 亞洲只能載入上海的timezone???
SELECT toDateTime('2019-07-31 10:10:10', 'Asia/Shanghai') as date_shanghai;


-- 計算連個時刻在不同時間單位下的差值
-- 第一種:指定時間計算差值示例
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;

-- timeSlot(StartTime, Duration, [,Size])
-- 它返回一個時間陣列,其中包括從從“StartTime”開始到“StartTime + Duration 秒”內的所有符合“size”(以秒為單位)步長的時間點
-- 作用:搜尋在相應會話中綜合瀏覽量是非常有用的。
SELECT
timeSlots(toDateTime('2012-01-01 12:20:00'), toUInt32(600)) as dateTimeArray,
dateTimeArray[0] as arr_index_0, -- no result.
dateTimeArray[1] as arr_index_1, -- 2012-01-01 20:00:00
dateTimeArray[2] as arr_index_2, -- 2012-01-01 20:30:00
dateTimeArray[3] as arr_index_3, -- no result.
dateTimeArray[4] as arr_index_4; -- no result.
-- toUInt32(600) 表示之後間距20秒的時刻
SELECT
timeSlots(now(), toUInt32(600), 20) as dateTimeArray, -- 類似於:引用地址
dateTimeArray[0] as arr_index_0, -- no result.為什麼?
dateTimeArray[1] as arr_index_1,
dateTimeArray[2] as arr_index_2,
dateTimeArray[3] as arr_index_3,
dateTimeArray[4] as arr_index_4,
dateTimeArray[5] as arr_index_5;
-- 指定時間為基準,之後每個元素增加20秒
SELECT
timeSlots(toDateTime('2012-01-01 12:20:00'), toUInt32(600), 20) as cur_dateTimeArray, -- 類似於:引用地址
cur_dateTimeArray[0] as arr_index_0, -- no result.為什麼?
cur_dateTimeArray[1] as arr_index_1, -- 2012-01-01 20:20:00
cur_dateTimeArray[2] as arr_index_2, -- 2012-01-01 20:20:20
cur_dateTimeArray[3] as arr_index_3, -- 2012-01-01 20:20:40
cur_dateTimeArray[4] as arr_index_4, -- 2012-01-01 20:21:00
cur_dateTimeArray[5] as arr_index_5; -- 2012-01-01 20:21:20