I who like to find a solution in T-SQL that could find a a way to detect a change in a given list or records.
The physical table is like this:
| id |val |
|----|----|
| 1 | A |
|----|----|
| 2 | A |
|----|----|
| 3 | B |
|----|----|
| 4 | B |
|----|----|
| 5 | A |
|----|----|
| 6 | A |
|----|----|
id is a sequencial integer
val is an arbitrary value
I would like to add an calculated field that could somehow denote a change of val
Desired result:
| id |val | segment |
|----|----|---------|
| 1 | A | 1 |
|----|----|---------|
| 2 | A | 1 |
|----|----|---------|
| 3 | B | 2 |
|----|----|---------|
| 4 | B | 2 |
|----|----|---------|
| 5 | A | 3 |
|----|----|---------|
| 6 | A | 3 |
|----|----|---------|
What I’m trying to do is the possibility to group by “segments” like this:
| from_id | to_id | val |
|---------|-------|-----|
| 1 | 2 | A |
| 3 | 4 | B |
| 5 | 6 | A |
|---------|-------|-----|
Assuming SQL Server 2005+
Or the updated requirement is a bit simpler