Age16_20 AgeBelow_16 Age21_30 Age31_40 Age41_50 Age51_60 QID
93 81 55 46 54 48 1
13 11 15 16 14 18 2
I want to subtract second row from first? which is best way to do inside stored procedure. performance main issue here so please provide optimal solution.
Assumes only 2 rows.
If more then 2 rows (why?) then change the CASE to
Assuming you have a unique identifier on the rows (we'll presume it is ID) you could do something like (I have only done the first column, for brevity):
If possible, however, you would be much better off storing the QID2 values as negatives. That way you don't need to know which one to subtract from the other; it's just a straight SUM.