Skip to content
Advertisement

Select and skip rows by date range with checking difference between them

I’m new in Teradata and i have a small sql problem, similar to this one below :

source table A:

output wanted:

Explanation:

1 –> if c is different (between current row and the next one) so dt_f of the current row = dt of the next row – 1 day , the two rows are selected

2–> if months_between(dt,dt) > 1 ( in the exmple between the row number 4 and the row number 5) so dt of the first row selected with same id a,b and c will be df ( of row number 4) + 1 month and the row 5 will be selected with dt_f = 31/12/2999.

I tried a lot using with recursive but i didn’t get the real result, but i believe it can be solved with it.

thank’s for your replies 🙂

Advertisement

Answer

If this was just about returning overlapping rows, it would be really simple using Teradata’s NORMALIZE extension:

But your logic to adjust the end date needs Analytic Functions. This is probably the simplest query to get those overlapping periods plus additional columns, modified to match your logic:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement