Find date difference between consecutive rows in Teradata

Calculate date difference between consecutive rows

Sometime we will get the requirement to calculate the date difference for consecutive rows within single column in Teradata. Since we need to find the date difference within a same table,we have to perform the join operation for the same table itself.

Sample table structure in Teradata

Query to find the date difference for consecutive rows

Explanation:

Same customer placing the order in two different dates and it has stored in the table Order_History.The E-commerce website wants to find the date difference between two orders for analytical purpose.

In that case,we can use the join for the Order_History table itself and validate second order date is greater than the first order date.Then it is easy to subtract the dates to find the difference between two order dates.

Output:

Date difference for consecutive rows