hive presto sql從身份證號提取年齡和性別

hive presto sql如何從身份證號提取年齡和性別
原理:採用substr把身份證18位的數字拆分得到想要的,並通過case when、cast as賦值爲想要的結果

select 2020-cast(substr(id,7,4)as int))*1.0 as age
平均年齡avg(2020-cast(substr(id,7,4)as int))*1.0 as age) as avg_age

select distinct case when cast(substr(id,17,1)as int) in (1,3,5,7,9) then male end
男性比例:
count (select distinct case when cast(substr(id,17,1)as int) in (1,3,5,7,9) then id end)*1.0000/count (distinct id) as male_rate

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章