Skip to content
Advertisement

SQL Server : calculate column value based on value in previous row/id

I have what I hope is a simple problem to solve, but one that is puzzling me. My table can be created with the below sql:

Which looks like this:

enter image description here

This is basically a currency conversion task (the costs in the es-mx row are arbitrary in this example). What I wish to do is update all the TotalProjectCost values of rows with lang of ‘es-mx’: for these rows, I wish to calculate the value as whatever the TotalProjectCost value is of the previous ‘en’ row * 15.8735.

The way my actual table is set up, values are always submitted in pairs (one en, one es-mx)…so odd Ids should always be en, even ones should always be es-mx. As you can see, there’s probably tons of ways to approach this and maybe that’s why it’s hard for me to know where to start.

I was kinda getting somewhere with this select statement:

But I still fall short of actually getting to the Update that calculates the correct values.

I appreciate any thoughts on this! I’ve tried looking around for an example on the boards that somewhat matches my scenario, but they seem a bit more involved than what I’m trying to accomplish.

Advertisement

Answer

Use LAG() widow function in an updatable CTE:

See the demo.
Results:

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