Subtract record from previous record within same column in teradata

1.4k views Asked by At

Suppose there is column as shown below.

enter image description here

How to subtract record from previous record. for example the first record from second record and so on and then store in third column. As these are date values I want results in this format: 1d:1h:46m

Thanks

1

There are 1 answers

0
Andrew On

You can use PRECEDING:

select
<key>,
(<your column> - PreviousValue) DAY(4) TO MINUTE
from
(
select
<some sort of key>,
    <your column>,
    min(<your column>) over (partition by <whatever your grain is> ORDER BY <whatever> ROWS BETWEEN 1 PRECEDING and 1 PRECEDING) as PreviousValue
    from <your table>
    ...
    ) t1

If you don't need to partition by anything, just leave out the partition by