I have a table like this
| UserID | Score | Date |
|---|---|---|
| 5 | 6 | 2010-1-1 |
| 7 | 8 | 2010-1-2 |
| 5 | 4 | 2010-1-3 |
| 6 | 3 | 2010-1-4 |
| 7 | 4 | 2010-1-5 |
| 6 | 1 | 2010-1-6 |
I would like to get a table like this
| UserID | Score | RunningTotal | Date |
|---|---|---|---|
| 5 | 6 | 6 | 2010-1-1 |
| 5 | 4 | 10 | 2010-1-3 |
| 6 | 3 | 3 | 2010-1-4 |
| 6 | 1 | 4 | 2010-1-6 |
| 7 | 8 | 8 | 2010-1-2 |
| 7 | 4 | 12 | 2010-1-5 |
Thanks!
Unlike
Oracle,PostgreSQLand evenMySQL,SQL Serverhas no efficient way to calculate running totals.If you have few scores per
UserID, you can use this:, however, this will be very inefficient for larger tables.
For larger tables, you could benefit from using (God help me) a cursor.