简体   繁体   English

如何从蜂巢中的记录中获取倒数第二个日期?

[英]How to get the second last date from records in hive?

I have a table like this: 我有一张这样的桌子:

    member        dato
    696382      2016-06-21
    35546232    2016-02-01
    9001107     2013-09-23
    40310785    2014-07-18
    3802508     2015-06-21
    74376545    2016-01-11
    14969202    2014-12-08
    17495001    2015-09-01
    17238917    2016-11-16

The dato is the date when a member buy a product which is from 2015-01-01 till yesterday. dato是会员购买产品的日期,从2015年1月1日到昨天。 I want to get a new table which contains three columns: member, dato, dato_second. 我想获取一个包含三列的新表:member,dato,dato_second。 dato_second is the date which is the most closest to the dato. dato_second是最接近于dato的日期。 For example, 17238917 has three datos in history in addtion to the '2016-11-16': '2016-11-10', '2015-03-27', the new record would be 17238917, '2016-11-16', '2016-11-10'. 例如,17238917在历史上除了'2016-11-16'之外还有三个datos:'2016-11-10','2015-03-27',新记录将是17238917,'2016-11-16 ','2016-11-10'。 So how to get the new table? 那么如何获得新表呢?

Hmmm. 嗯。 I think conditional aggregation and row_number() is the simplest solution: 我认为条件聚合和row_number()是最简单的解决方案:

select member,
       max(case when seqnum = 1 then dato end) as dato,
       max(case when seqnum = 2 then dato end) as dato_1
from (select t.*,
             row_number() over (partition by member order by dato desc) as seqnum
      from t
     ) t
group by member;

This is what you are looking for 这就是你要找的

select  member
       ,dato
       ,lag (dato) over (partition by member order by dato) as prev_dato            

from   mytab

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM