I have a table like this:
| current_date | user_id | mode_name | mode_time |
|---|---|---|---|
| 2021-10-01 | 1 | game | 10 |
| 2021-10-02 | 1 | game | 10 |
| 2021-10-02 | 1 | tv | 30 |
| 2021-10-09 | 1 | music | 10 |
| 2021-10-15 | 1 | music | 40 |
| 2021-10-01 | 2 | music | 10 |
| 2021-10-01 | 2 | game | 10 |
| 2021-10-04 | 2 | game | 10 |
| 2021-10-04 | 2 | music | 20 |
| 2021-10-05 | 2 | tv | 40 |
| 2021-10-11 | 2 | tv | 40 |
| 2021-10-12 | 2 | game | 20 |
And I want to add two columns:
- Column with favourite
mode_name, according cumulate sum ofmode_timecolumn for everyuser_id - Column with cumulate sum of
mode_timecolumn from favouritemode_namefor everyuser_id
The desired table should look like this:
| current_date | user_id | mode_name | mode_time | favourite_mode | favourite_mode_time |
|---|---|---|---|---|---|
| 2021-10-01 | 1 | game | 10 | game | 10 |
| 2021-10-02 | 1 | game | 10 | tv | 30 |
| 2021-10-02 | 1 | tv | 30 | tv | 30 |
| 2021-10-09 | 1 | music | 10 | tv | 30 |
| 2021-10-15 | 1 | music | 40 | music | 50 |
| 2021-10-01 | 2 | music | 10 | game | 10 |
| 2021-10-01 | 2 | game | 10 | game | 10 |
| 2021-10-04 | 2 | game | 10 | music | 30 |
| 2021-10-04 | 2 | music | 20 | music | 30 |
| 2021-10-05 | 2 | tv | 40 | tv | 40 |
| 2021-10-11 | 2 | tv | 40 | tv | 80 |
| 2021-10-12 | 2 | game | 20 | tv | 80 |
Table can be found here https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=e05302a2cfd81a2a55de811e294f513e