使用expiredate在hiveql中构建历史表
我需要在表的基础上构建som 带身份证和日期 每一个日期都标志着一个变化,最新的日期是 主动一号,使用expiredate在hiveql中构建历史表,hive,hiveql,Hive,Hiveql,我需要在表的基础上构建som 带身份证和日期 每一个日期都标志着一个变化,最新的日期是 主动一号, table historic_trans id trans_date 22 20170510 22 20170502 22 20170412 我想建立一个历史表,其中最新的一行 通过将expiredate列添加为“99991231”在active获取分数 我可以很容易地找到活动的 select id, max(trans_date)trans_date, '99991231'
table
historic_trans
id trans_date
22 20170510
22 20170502
22 20170412
我想建立一个历史表,其中最新的一行
通过将expiredate列添加为“99991231”在active获取分数
我可以很容易地找到活动的
select id, max(trans_date)trans_date, '99991231' as Expiredate, 'yes' as active
from historic_trans
where id = '22'
group by id
但是我真的需要在前一行设置trans_日期
在我的非活动行中
id trans_date Expiredate active
22 20170510 99991231 yes
22 20170502 20170510 no
22 20170412 20170502 no
因此,expiredate反映了交易中的变化
这可以在纯hql/sql中实现吗
我一直在玩下面的代码,但我被困在它
select historic_trans.id, historic_trans.trans_date,
case when aktiv.Expiredate = '99991231' then aktiv.Expiredate
else aktiv.Expiredate
end as Expiredate
from historic_trans
left outer join
(
select id, max(trans_date)trans_date, '99991231' as Expiredate, 'yes' as active
from historic_trans
where id = '22'
group by id
) aktiv on aktiv.id = historic_trans.id and aktiv.trans_date = historic_trans.trans_date
where historic_trans.id = '22'
有什么建议吗
select id
,trans_date
,lag (trans_date,1,date '9999-12-31') over w as Expiredate
,case when row_number () over w = 1 then 'yes' else 'no' end as active
from historic_trans
window w as (partition by id order by trans_date desc)
;
没有使用日期类型或至少ISO日期格式(yyyy-MM-dd)的任何原因?只要数据收到,我就可以用另一种格式指定ExpireDate。
+----+------------+------------+--------+
| id | trans_date | expiredate | active |
+----+------------+------------+--------+
| 22 | 2017-05-10 | 9999-12-31 | yes |
| 22 | 2017-05-02 | 2017-05-10 | no |
| 22 | 2017-04-12 | 2017-05-02 | no |
+----+------------+------------+--------+