sparksql
1.from_unixtime将时间戳换算成当前时间,to_unix_timestamp将时间转化为时间戳
SELECT from_unixtime(0, 'yyyy-MM-dd HH:mm:ss'); 1970-01-01 00:00:00
SELECT to_unix_timestamp('2016-04-08', 'yyyy-MM-dd'); 1460041200
2.将一列拼接的字符串转换为一行等同于regexp_split_to_table
explode(split(substr(orginal_berth,1,LENGTH (orginal_berth) - 1),'[|]'))
需要注意的是以上函数不能使用sum、count等聚合操作。可以看做一个表在进行聚合操作。
3.substr截取字符串,substring_index
SELECT substring_index('www.apache.org', '.', 2); www.apache
SELECT substring_index('www.apache.org', '.', -1); org
postgresql
1.一行数据中的多字段值根据连接符拼接
concat_ws('||',aaa,bbb); aaa||bbb
2.计算某个字段的长度,string类型的输入和输出
length("aaa") = '3' 字段::text 转化为string类型
3.case when then else end的语句的使用
sum(case when "sex" =1 then "population" else 0 end) as 男
4.时间和时间戳的相互转换
to_char(timestamp, text) text 把时间戳转换成字串 to_char(current_timestamp, 'HH12:MI:SS')
yyyy-MM-dd HH24-MI-SS
to_timestamp(text, text) timestamp 把字串转换成时间戳 to_timestamp('05 Dec 2000', 'DD Mon YYYY')
5. 将null替换为自定义值
coalesce(字段名,替换值)
6.删除表中数据
truncate table tablename
7.substring 截取任意长度的子字符串
substring(string [from int] [for int])
8.按照某个符号进行拆分形成一行
regexp_split_to_table(字段,拆分符)
9.split_part拆分取下标
split_part('a-b','-',1) a
10.合并两个相反的字符串
WITH A AS (
SELECT
split_part('tianjin-shanghai', '-', 1) AS relation
),
b AS (
SELECT
split_part('tianjin-shanghai', '-', 2) AS relation
) SELECT
CASE
WHEN A .relation < b.relation THEN
A .relation || b.relation
ELSE
b.relation || A .relation
END re
FROM
A,
b