I am doing this query, I want to be able to take 2 values from different rows from 1 column(Population growth rate) subtract them from each other, and put that value into the new column(PopGrowthRateDiff), making it repeat all the way down, making a null for the 1st value in this new column(since it has no previous number) for each new title/word in the 1st row(Country name). I likely overcomplicated this, or completely did it wrong, any help is greatly appreciated!
this is the result I want within an SQL database:
https://www.db-fiddle.com/f/4o1Z96ADgMEDXT1uz1Sn2D/1starting from:
https://www.db-fiddle.com/f/jsFB3ELxiX8xp6Xghk21Fe/1select FirstSet.[Country name], FirstSet.Year, FirstSet.[Population growth rate], FirstSet.[Life expectancy at birth], Sum(SecondSet.PopGrowthRateDifference2 - FirstSet.PopGrowthRateDifference1) as PopGrowthRateDiff from ( Select gro.[Country name], gro.Year, gro.[Population growth rate], expe. [Life expectancy at birth], SUM( gro.[Population growth rate]) OVER (partition by gro.[Country name] order by gro.[Country name], gro.Year) as PopGrowthRateDifference1 From PortfolioProject..PopulationGrowthRate$ gro Join PortfolioProject..LifeExpectancy$ expe on gro.[Country name] = expe.[Country name] and gro.Year = expe.Year ) as FirstSet inner join ( Select gro.[Country name], gro.Year, gro.[Population growth rate], expe.[Life expectancy at birth], SUM(gro.[Population growth rate]) OVER (partition by gro.[Country name] order by gro.[Country name], gro.Year) as PopGrowthRateDifference2 From PortfolioProject..PopulationGrowthRate$ gro Join PortfolioProject..LifeExpectancy$ expe on gro.[Country name] = expe.[Country name] and gro.Year = expe.Year ) as SecondSet on FirstSet.[Country name] = SecondSet.[Country name] and FirstSet.Year = SecondSet.Year and FirstSet.[Population growth rate] = SecondSet.[Population growth rate] group by FirstSet.[Country name], FirstSet.Year, FirstSet.[Population growth rate], FirstSet.[Life expectancy at birth] order by FirstSet.[Country name], FirstSet.Year
This is my code as of now, it takes 2 queries and treats them as tables, both queries are rolling counters that reset at each new 'Country name'. Afterward, it subtracts them, which gives 0, the db-fiddle shows what I actually want to happen.