I'm using sql and have data coming from redshift. The data is cumulative, so It keeps adding the previous total and the new total which is why the graph is linear. I want to subtract each row by the...

1 answer below »
I'm using sql and have data coming from redshift. The data is cumulative, so It keeps adding the previous total and the new total which is why the graph is linear. I want to subtract each row by the previous row in order to get the monthly values instead of cumulative value. For example, for 8-2022 the value should be 13 million instead of 34.7. So I would want the 8-2022 column to subtract from the 7-22 column to get the correct value.

Answered 1 days AfterJun 07, 2022

Answer To: I'm using sql and have data coming from redshift. The data is cumulative, so It keeps adding the...

Mohd Abas answered on Jun 08 2022
80 Votes
Syntax:
LAG([,offset[, default_value]]) OVER (
PARTITION BY expr,...
ORDER BY
expr [ASC|DESC],...
);
SQL provides us with the lag function which allows us to fetch values of previous row from the current row.
The LAG() function evaluates the expression for current row and the offset specifies that how many previous rows we have to consider for the current query.
It can also be used with partitions as well .
For the screenshot provided the SQL could be as;
With months...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here