I have a simple table with some dummy data settings, for example:
|id|user|value|
---------------
1 John 2
2 Ted 1
3 John 4
4 Ted 2
I can select the current amount by executing the following SQL query (MSSQL 2008):
SELECT a.id, a.user, a.value, SUM(b.value) AS total
FROM table a INNER JOIN table b
ON a.id >= b.id
AND a.user = b.user
GROUP BY a.id, a.user, a.value
ORDER BY a.id
This will give me results like:
|id|user|value|total|
---------------------
1 John 2 2
3 John 4 6
2 Ted 1 1
4 Ted 2 3
Now you can only get the latest lines for each user? Thus, the result will be:
|id|user|value|total|
---------------------
3 John 4 6
4 Ted 2 3
Am I going to do it right? Any suggestions or a new way to watch would be great!
source
share