Home > database >  SQL subtraction problem
SQL subtraction problem

Time:09-22

One table, one column of the data line want to lose,
Such as A table
Pid pNum
1 5
2 10
3 20

It is best to use SQL statements, the result of the 20-10-5, from the bottom, in turn, is presupposed, and the data line is not fixed,

You have any good method, the younger brother novice, warrior, please help

CodePudding user response:

Pid pNum
1-5
2-10
3 20

Handle like this

CodePudding user response:

So to imagine the last line of the not, then the SUM of the column

CodePudding user response:

Use cursor, take when data is arranged in reverse chronological order, and then how to deal with, is very simple

CodePudding user response:

Select sum (pnum)
The from (
Select - 1 * sum (pnum) pnum
The from tablename
Union all
Select 2 * pnum pnum
The from tablename
Where pid=(select Max (pid) from tablename)
) tabletmp
  • Related