I have an external value 20. I need 1 to be subtracted from 20 (=> 19) and then 2 to be subtracted from 19 (=> 17) and so on.
Data:
Serial No | Value1 | External value(20)
A | 1 | 19
B | 2 | 17
C | 3 | 14
D | 4 | 10
E | 5 | 5
How can I do it?
Best Answer
I supposed that the third column is the result of your subtraction and doesn't exist in your main table.
Query:
The query uses a variable which is set to 20 at the beginning, cross join it to the data table and subtract each
Value1
one by one.Value1
are order bySerial
, from A to E. The order seems to be A to E but based on real data, you will have to order it according to your needs.Output:
Sample (sqlfiddle):