I’m using MS SQL Server 2008, and I would like this:
+------+--------+--------+------------+
| id | Name | Event | Timestamp |
+------+--------+--------+------------+
| 0 | bob | note | 14:20 |
| 1 | bob | time | 14:22 |
| 2 | bob | time | 14:40 |
| 3 | bob | time | 14:45 |
| 4 | bob | send | 14:48 |
| 5 | bob | time | 15:30 |
| 6 | bob | note | 15:35 |
| 7 | bob | note | 18:00 |
+------+--------+--------+------------+
To become this:
+------+--------+--------+------------+
| id | Name | Event | Timestamp |
+------+--------+--------+------------+
| 0 | bob | note | 14:20 |
| 1 | bob | time | 14:22 |
| 4 | bob | send | 14:48 |
| 5 | bob | time | 15:30 |
| 6 | bob | note | 15:35 |
+------+--------+--------+------------+
I.e., rows are “grouped” by the “event” column. Only one of each grouped identical “event” is to be shown.
- If one event, e.g. “note” as with id 0, is in the table with no row directly before or after it (row with nearest timestamps) that has an equal “event” value, it is shown;
- If more than one row with the same event, e.g. “time” as with id 1-3. comes after each others (i.e. no row with a different “event” has a timestamp that is “between” them), any one of them is shown (doesn’t matter for me, all other columns are identical anyway).
Those two are the only rules.
If
ids are one after one try do it this way:result:
Added:
If
ids aren’t one after one, you can make them to be: