HiveSQL-历史数据汇总

现在有员工的职位历史变动数据

tableA 数据表字段:emp_id ,pos_id,pos_nm,job_id,date

其中date是yyyymmdd形式存放的日期

怎么将每个月的数据汇总,汇总成字符串 “0101010” 的形式?字符串的长度是这个月天数的总日期,比如1月份就有31个字符,2月是29或者28个字符?

SQL

with base as (
select 
emp_id,pos_id,pos_nm,job_id,year_month,date
,case when month_days == 31 then CONCAT(SUBSTR('0000000000000000000000000000000', 1, index-1), '1', SUBSTR('0000000000000000000000000000000', index+1))
when month_days == 30 then CONCAT(SUBSTR('000000000000000000000000000000', 1, index-1), '1', SUBSTR('000000000000000000000000000000', index+1))
when month_days == 29 then CONCAT(SUBSTR('00000000000000000000000000000', 1, index-1), '1', SUBSTR('00000000000000000000000000000', index+1))
when month_days == 28 then CONCAT(SUBSTR('0000000000000000000000000000', 1, index-1), '1', SUBSTR('0000000000000000000000000000', index+1))
end
date_bit
 from (
    select  emp_id  ,pos_id,pos_nm,job_id
    ,concat(year ,"-", month,"-",datt) as date
    ,concat(year,"-",month)  as year_month 
    ,day(concat(year,"-",month,"-",datt)) as index --这一天是第几天
    ,day(last_day(concat(year ,"-", month,"-",datt))) as month_days --一个月有多少天
    from(
         select emp_id  ,pos_id,pos_nm,job_id
         , substring(date,0,4) as year, substring(date,5,2) as month, substring(date,7,2) as datt 
        from tableA
        )
    )
)
select *,reverse(date_bit) from base 
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值