Lets say we have a table and I want to create a column similar to the "Damage Taken".
If the name of the row above is the same as this column, subtract last rows damage with this row. Else, subtract Life from this row subtract with the Damage of this row.
Name Damage Life Damage Taken
Bill 97 100 -> (100 - 97) = 3
Bill 93 100 -> (97 - 93) = 4
Bill 71 100 -> (93 - 71) = 22
Bill 54 100 -> (71 - 54) = 17
Stacy 112 200 -> (200 - 112) = 88
Stacy 109 200 -> (112 - 109) = 3
Stacy 91 200 -> (109 - 91) = 18
Stacy 81 200 -> (91 - 81) = 10
Stacy 62 200 -> (81 - 62) = 19
I'm relatively new to SQL and think in terms of C++ with the underlying logic of the column like so.
for(int i = 0; i<Name.size(); i++)
if(Name[i] == Name[i-1]){
damageTaken[i] = Damage[i-1] - Damage[i];
}
else{
damageTaken[i] = Life[i] - Damage[i];
}