clickhouseSQL日期相关

1. 毫秒级时间戳转日期/小时

sql 复制代码
--13位时间戳转具体时间
toDateTime(report_time / 1000) as _c00 

-- 获取时间戳对应的时间点整点(结果:%Y-%m-%d %H:00:00.0) eg:2022-09-28 23:00:00.0
toStartOfHour(toDateTime(report_time / 1000)) AS _10

-- 获取时间点整点(结果无日期:%H)eg:23
toHour(FROM_UNIXTIME(report_time div 1000)) as _100

-- 13位时间戳转成年月日时(结果:%Y-%m-%d %H)
FROM_UNIXTIME(report_time div 1000,'%Y-%m-%d %H') 


-- 获取时间戳对应的日期开始时间(结果:%Y-%m-%d 00:00:00.0)eg:2022-09-28 00:00:00.0
toStartOfDay(toDateTime(report_time / 1000)) AS _02

--获取时间戳对应的日期(结果:%Y-%m-%d)eg:2022-09-28
toDate(toDateTime(report_time / 1000)) AS _01
toDate(toStartOfDay(toDateTime(report_time / 1000))) AS _0

-- 获取n分钟间隔(eg,10分钟间隔指00-09,10-19......55-59)
toStartOfInterval(toDateTime(report_time / 1000), INTERVAL 10  minute)

-- 字符串时间转datetime
toDateTime('2023-02-02 01:02:15')

结果如下:

● "/"和"div"的区别

"/"非整除,结果仍然是浮点数

"div"为整除,得到10位数结果

2. 当前时间往前推

解决凌晨0点之后,例行时间是跑前一日23点数据的问题

sql 复制代码
-- 获取当前时间
now()

-- 当前时间前推1小时
date_sub(hour, 1, now())

-- 再转换为日期
toDate(date_sub(hour, 1, now()))

-- 前推n天
date_sub(day,7,toDate(date_sub(hour,1,now())))--前推7天

-- 当前时间往前推1小时,并转化为int8格式
toInt8(substring(cast(date_sub(hour, 1, toDateTime('2023-02-02 01:02:15')) as String), 12, 2))
toInt8(substring(CAST(minus(toDateTime('2023-02-02 01:02:15'), toIntervalHour(1)), 'String'), 12, 2))
-- toDateTime('2023-02-02 01:02:15') = now()
相关推荐
mpHH5 分钟前
babelfish for postgresql 分析--babelfishpg_tds--doing
数据库·postgresql
档案宝档案管理1 小时前
档案管理系统如何对企业效率重构与提升?
大数据·数据库·人工智能·重构·档案·档案管理
TimberWill1 小时前
PostgreSQL表备份并重命名出现索引、外键仍指向旧表,恢复后仍失败的问题
数据库·postgresql
沐浴露z2 小时前
MySQL MVCC:通过 ReadView与 undolog版本链 分析读提交RC和可重复读RR的原理
数据库·mysql·innodb·mvcc
阿巴~阿巴~2 小时前
MySQL索引特性(重点)
服务器·数据库·sql·mysql·ubuntu
NiNi_suanfa2 小时前
【MySQL数据库】InnoDB实现MVCC(多版本并发控制)底层原理
数据库·mysql
qq_172805592 小时前
好用的 SQLite3 ORM 开源库
数据库·sqlite·开源
AWS官方合作商2 小时前
打破数据枷锁:在AWS上解锁Oracle数据库的无限潜能
数据库·oracle·aws
粟悟饭&龟波功3 小时前
【网络安全】四、中级篇:SQL注入详解
sql·安全·web安全
会飞的架狗师3 小时前
【MySQL体系】第2篇:MySQL索引类型和原理
数据库·mysql