How Do you add/substract from previous row in SQL?
In Mysql
Table
Id AA BBB
A 45 123
B 52 120
C 40 135
How would I get
B_A 7
A_C 5
First would need to sort by BBB Asc then minus 52-45. B_A concatenate B and A
How Do you add/substract form previous row in SQL?
If I needed to ha开发者_运维百科ve seperate cols for B then A, How Would I add this.
SELECT concat(t1.id , '_', t2.id, ' ', t1.aa - t2.aa )
FROM table t1
INNER JOIN (SELECT Max(t2.id) prev_id,
t1.id
FROM table t1
INNER JOIN table t2
ON t1.id > t2.id
GROUP BY t1.id) prev
ON t1.id = prev.id
INNER JOIN table t2
ON t2.id = prev.prev_id
精彩评论