Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ucal
Yak Posting Veteran
72 Posts |
Posted - 2008-11-09 : 08:43:12
|
| Cosider the table below.ID X Y -- -- -- 1 a 112 a 123 a 144 b 105 b 166 c 77 c 8The Y values of record 3 and 5 are out of sequence . They should have been 13 and 11 respectively.The idea is to Flag cases that are out of sequence within the same group of YMy desired result is as followsID X Y Y2 Flag-- -- -- -- ----1 a 11 11 12 a 12 12 13 a 14 13 04 b 10 10 15 b 16 11 06 c 7 7 17 c 8 8 1Any assistance will be most welcomed |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-09 : 11:32:20
|
| [code]SELECT t.ID, t.X, t.Y, CASE WHEN t.Y<>t1.Y+1 THEN t1.Y+1 ELSE t.Y END AS Y2, CASE WHEN t.Y<>t1.Y+1 THEN 0 ELSE 1 END AS FlagFROM Table tOUTER APPLY(SELECT TOP 1 Y FROM Table WHERE X=t.X AND ID < t.ID ORDER BY ID DESC) t1[/code] |
 |
|
|
ucal
Yak Posting Veteran
72 Posts |
Posted - 2008-11-09 : 17:39:21
|
| Thanks visakh16 , that worked like Charm !!!!!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-09 : 23:23:18
|
Cheers |
 |
|
|
|
|
|