Skip to content
Advertisement

Create column with timeframe relative to other column in SQL

Suppose I have the following table t_1 where every row represents a day:

How can I create the following table t2 from t1?

Here val_cur is defined as the sum of values of the current timeframe (i.e. the sum of values between date_start and date_end) and val_prev is defined as the sum of values of the previous timeframe (i.e. the current timeframe minus one week).

Output:

But how do I get the last column?

Advertisement

Answer

Consider below approach

with output

enter image description here

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