简体   繁体   中英

How to get the second last partition in hive (HQL)

I have a hive table with below partitions:

part_date='07-12-2016'

part_date='04-12-2016'

part_date='01-12-2016'

Now how can I get the first partition which is less than the most recent partition in hive query. Assuming the partition values are the date when the data is ingested into the table. In this case it should be part_date='04-12-2016'.

so the query should be like:

select part_date from table_A where part_date > second_last_partition(ie 04-12-2016);

Thanks in advance

You can use the row_number window function and get the second latest row.

select part_date
from (select part_date,row_number() over(order by part_date desc) as rn 
      from table_A) t
where rn = 2

I assume the part_date column is distinct. If not, you should use the dense_rank function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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