不同数据库的同功能函数总结

时间相关

时间的加减

Mysql:timestamp + interval n minute/hour/day/month/year,如:"2022-04-20 00:00:00" + interval 1 hour

postgresql: timestamp + '+1 month',如: to_date(to_char(data_date::timestamp + '+1 month', 'yyyy-mm-dd'), 'yyyy-mm-dd')

spark sql: date_add/add_months等

时间转换为字符串

Mysql:timestamp 本来就可以当字符串用

postgresql: to_char(date,‘YYYY’)

字符串转换为时间

to_date(cast("datecode" as varchar),'yyyyMMdd') as "datecode"

规范时间格式

Mysql:date_format(timestamp , '%Y-%m-%d %H:%i:%s')

postgresql: to_char(date,‘YYYY’)

date_trunc('month',now()) 获取当前月份的首日分秒

extract(month from now()) = date_part('month',now()) 获取当前日期的月份数字



聚合函数相关

排序

Postgresql: row_number() over (partition by A order by B asc(desc)) 

多行的数据以字符串的形式连接为一行

Postgresql: array_to_string(array_agg(DISTINCT "titleCh" order by "titleCh" desc),'') as titleChs

Mysql: GROUP_CONCAT(live_room_id order by live_room_id SEPARATOR ', ') as live_room_id

一行数据按照字符切割拆成多行

Postgresql: regexp_split_to_table(字段, 符号)

开窗函数

Postgresql: 聚合函数 over (partition by __ order by __)

Mysql: 无开窗函数。替代方法:GROUP_CONCAT+SUBSTRING_INDEX结合可以组内排序取最值,或自身关联一遍(MySQL实现over partition by(分组后对组内数据排序)_MrCao杰罗尔德的博客-CSDN博客_mysql over partition)

如:SUBSTRING_INDEX(GROUP_CONCAT(d.monitor_time ORDER BY d.monitor_time asc ),',',1)  group by d.date, d.hour 取每小时中monitor time排第一的

你可能感兴趣的:(不同数据库的同功能函数总结)