Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

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 XXXXXXXXXXthe value should be 13 million instead of 34.7. So I would want the XXXXXXXXXXcolumn to subtract from the 7-22 column to get the correct value.
Answered 1 days After Jun 07, 2022

Solution

Mohd Abas answered on Jun 08 2022
94 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 cu
ent row.
The LAG() function evaluates the expression for cu
ent row and the offset specifies that how many previous rows we have to consider for the cu
ent 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