| Author |
Topic |
|
ucal
Yak Posting Veteran
72 Posts |
Posted - 2008-11-11 : 22:41:11
|
| Have the following data sets with results below (Thanks to visakh16)declare @t table (X int, Y int, z int)insert into @tselect 1, 1, 11 union allselect 2, 2, 11 union allselect 3, 3, 11 union allselect 4, 5, 11 union allselect 5, 6, 11 union allselect 6, 7, 11 union allselect 7, 8, 11 union allselect 8, 4, 22 union allselect 9, 6, 22 union allselect 10, 8, 22 union allselect 11, 9, 22 union allselect 12, 3, 33 union allselect 13, 4, 33 union allselect 14, 7, 33 union allselect 15, 9, 55 union allselect 16, 10, 55 select a.x ,a.z ,a.y as Y1 ,CASE WHEN a.Y<>b.Y+1 THEN 0 ELSE 1 END AS wfrom @t aouter apply(select top 1 y from @t where z = a.z and x < a.x order by x desc)bx z y w1 11 1 12 11 2 13 11 3 1 <---This is 1 because from 2 to 3 within4 11 5 0 Z (11) is continues5 11 6 16 11 7 17 11 8 18 22 4 1<-----Will like to set this to 0 because9 22 6 0 from 4 to 6 within same group(22) of Z 10 22 8 0 is not continues(missing 5) 11 22 9 1 12 33 3 113 33 4 114 33 7 015 55 9 116 55 10 1Any assistace on how to update the query to accomodate this will be most welcomed |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-11 : 23:09:09
|
| [code]declare @t table (X int, Y int, z int)insert into @tselect 1, 1, 11 union allselect 2, 2, 11 union allselect 3, 3, 11 union allselect 4, 5, 11 union allselect 5, 6, 11 union allselect 6, 7, 11 union allselect 7, 8, 11 union allselect 8, 4, 22 union allselect 9, 6, 22 union allselect 10, 8, 22 union allselect 11, 9, 22 union allselect 12, 3, 33 union allselect 13, 4, 33 union allselect 14, 7, 33 union allselect 15, 9, 55 union allselect 16, 10, 55 select a.x,a.z,a.y as Y1,CASE WHEN a.Y<>ISNULL(b.Y+1,c.Y-1) THEN 0ELSE 1END AS wfrom @t aouter apply(select top 1 yfrom @t where z = a.z and x < a.xorder by x desc)bouter apply(select top 1 yfrom @t where z = a.z and x > a.xorder by x)coutput--------------------------------x z Y1 w1 11 1 12 11 2 13 11 3 14 11 5 05 11 6 16 11 7 17 11 8 18 22 4 09 22 6 010 22 8 011 22 9 112 33 3 113 33 4 114 33 7 015 55 9 116 55 10 1[/code] |
 |
|
|
ucal
Yak Posting Veteran
72 Posts |
Posted - 2008-11-12 : 07:54:29
|
| Thanks ,I will want w for record 4 and 10 to be 1 because 5 to 6 within z (11) and 8 to 9 within z (22) and both continues |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-12 : 08:54:34
|
quote: Originally posted by ucal Thanks ,I will want w for record 4 and 10 to be 1 because 5 to 6 within z (11) and 8 to 9 within z (22) and both continues
i didnt understand what you're asking here. i gave you solution as per sample output you posted. Are you saying that this is not what you want? if yes, why didnt you illustrate this in sample data posted? |
 |
|
|
ucal
Yak Posting Veteran
72 Posts |
Posted - 2008-11-12 : 20:41:08
|
| visakh16, I was complicating an otherwise solution you provided.I am all set, thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-12 : 23:13:04
|
Cheers |
 |
|
|
|
|
|