Hello i have the following dataset
id price amount
1 10 1
2 20 2
3 20 1.5
4 21 1
5 21 2
SELECT amount, price, (@CumulativeSum := @CumulativeSum + amount) AS CumSum
FROM orderbook
And it is working fine. I would like to populate my python dictionary using also a "GROUP BY price" clause but this is affecting my final result. The final data should show the SUM of the amounts grouped by price.
I tried the following queries
SELECT amount, price, (@CumulativeSum := @CumulativeSum + amount) AS CumSum
FROM orderbook
GROUP BY price
SELECT SUM(amount), price, (@CumulativeSum := @CumulativeSum + amount) AS CumSum
FROM orderbook
GROUP BY price
SELECT amount, price, (@CumulativeSum := @CumulativeSum + SUM(amount)) AS CumSum
FROM orderbook
But the cumulative sum or the grouped sums are always wrong.
The final result should be a simple order book for a financial market.
The desired output is
price amount CumSum
10 1 1
20 3.5 4.5
21 3 7.5
Thanks for the hints
question from:
https://stackoverflow.com/questions/65925372/mysql-query-group-by-and-cumulative-sum-for-financial-orderbook 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…