SQL

总结一下SQL几种用法。

1.取百分数并保留两位小数

cast(round((y.一手单笔数)/(申请笔数*1.0) * 100.00,2) as varchar(5))||'%' 

2.出生日期转换为年龄

year(from_days(datediff(now(), a.birthday))) AS 年龄

3.嵌套json:提取mobile_address字段

json表达式为:{"INFOANALYSIS":{"id_age":"38","address_detect":{"mobile_address_city":"唐山市","id_card_city":"唐山市","mobile_address":"河北省唐山市","id_card_province":"河北省","mobile_address_province":"河北省","id_card_address":"河北省唐山市丰润县"},}}

replace(json_extract(JSON_EXTRACT(json_extract(a.reason_desc, '$.INFOANALYSIS'), '$.address_detect'), '$.mobile_address'), '"', '') AS 手机号归属地省市

注意:解json如果表达式里面为空的话就会报错,要加上 where len(a.reason_dsc) != 0作为条件

4.去重:request_id重复,有insert_time可区分,应取insert_time最大值为准

select * from (select request_id,max(insert_time) as insert_time from ams_request group by request_id) m left join ams_request n on m.request_id=n.request_id and m.insert_time = n.insert_time

5.窗口函数

SELECT *, Row_Number() OVER (partition by deptid ORDER BY salary desc) rank FROM employee


你可能感兴趣的:(SQL)