Ma28
Ma28

Reputation: 111

Is there function to get n-1 value from next row in Hive?

I need to get the date-1 value of next row date field. Need result similar to column "Expected Date".

enter image description here

I tried lag function but not getting the expected result.

Upvotes: 0

Views: 450

Answers (1)

Rishu S
Rishu S

Reputation: 3968

Try using hive lead function. Lead function give you the next row date value. Once you get the next row, use date_add or date_sub to subtract 1 day.

SELECT
Date,
coalesce(date_add(lead(Date,1) over(order by Date), -1),'9999-12-31') as expected_date
FROM
table

Finally, you can use coalesce to default the final row value to 9999-12-31 for the null row.

Upvotes: 1

Related Questions