
一、字符串函数-- 字符串长度 select length(hello) -- 替换 select replace(hello,l,x) -- 截取 select substr(hello,2,3) -- 拼接 select concat(hello,hello,hello) select hello||hello||hello select concat(id,name)from hero -- 拼接时添加分隔符, 分隔符写在第一个参数 select concat_ws(,,id,name,id) from hero --collect_set 聚合后去重 | collect_list 聚合后不去重 select province_id, collect_set(city_name) s,collect_list(city_name) l from city group by province_id -- 去除空格 select TRIM( dsdsa ) -- 转换大写 a A select upper(xxx) --大写 select lower(AAA) --小写二、日期函数-- 提取日期 --年 select substr(CURRENT_TIMESTAMP,1,4) select year(CURRENT_TIMESTAMP) --月 select substr(CURRENT_TIMESTAMP,6,2) select month(CURRENT_TIMESTAMP) --天 select day(CURRENT_TIMESTAMP) --小时 select hour(CURRENT_TIMESTAMP) -- bigint类型 的时间戳的转换 select from_unixtime(1782972840,yyyy-MM-dd HH:mm:ss) -- 日期间隔天数 select datediff(2020-10-01,2026-07-02) --前减后 select datediff(CURRENT_DATE,2020-10-01) -- 间隔月数 select months_between(CURRENT_DATE,2020-10-01) -- 前后N天 select date_sub(CURRENT_DATE,1) --往前1天 select date_add(CURRENT_DATE,1) --往后1天三、数值型函数mod --取余 CEIL --向上取整 FLOOR --向下取整 round --四舍五入四、转换函数nvl COALESCE IF case when五、窗口函数----------------- 窗口函数 函数()over(partition by xxx order by xxx rows between xxx and yyy) -- 排名开窗 -- 通常用于 计算 Top-N row_number() 1234 rank() 1134 DENSE_RANK() 1123 -- rows between xxx and yyy preceding : 向前找 following : 向后找 CURRENT row : 当前行(计算哪一行,该行就是当前行) unbounded : 一直到窗口的边界 rows between unbounded preceding and CURRENT row 2026-07-02,1256.3, 青岛 2026-07-03,892.7, 上海 2026-07-04,3691.5, 深圳 2026-07-05,745.2, 天津 2026-07-06,5208.9, 重庆 2026-07-07,1634.1, 杭州 2026-07-08,967.4, 苏州 2026-07-09,4120.6, 成都 2026-07-10,2351.8, 武汉 2026-07-11,689.3, 西安 -- 聚合开窗 -- 累计计算(累计求和, 累计平均) SUM avg max MIN count -- 偏移开窗: 将行和行的关系 转换成 列和列的关系 -- 计算 同比 环比 , 连续性问题 lag(要偏移的列,偏移量,默认值) lead 2026-07-02,1562.8 2026-07-03,739.4 2026-07-04,9241.6 2026-07-05,485.2 2026-07-06,3670.9 2026-07-07,1286.3 2026-07-08,597.7 2026-07-09,7135.1 2026-07-10,2048.5 2026-07-11,863.9 create table test(dt string, amt int)ROW FORMAT DELIMITED FIELDS TERMINATED BY ,; select dt,amt,lag(amt,1,0)over(order by dt) l from test -- 切片开窗 ntile -- 一般用于 百分比计算 ntile(数字) SELECT a.*, ntile(4)over(PARTITION BY deptno ORDER BY sal desc) FROM emp a