| Author |
Topic |
|
ucal
Yak Posting Veteran
72 Posts |
Posted - 2008-11-07 : 21:05:27
|
| I have a table with the following data elementsX Y Val1-- -- ----1 1 202 1 103 1 104 1 205 2 156 2 177 2 158 3 189 3 1810 3 1811 4 2012 4 2313 4 10Looking for a query that will return the followind data elements.X Y Val1 Val2-- -- ---- ----1 1 20 02 1 10 103 1 10 104 1 20 05 2 15 06 2 17 07 2 15 08 3 18 189 3 18 18 10 3 18 1811 4 20 012 4 23 013 4 10 0Val2 is 0 if within the same group of Y ,Val1 is not the same asthe next value of Val1 ,otherwise Val1 =Val2'Thus :IF Y = Y +1 and Val1 <> Val1 +1 THEN Val2 = 0ELSE Val2 =Val1 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-11-07 : 23:11:54
|
I'm not sure your desired results matches your requirement description. But here is a way to self join so that you can compare one value with the next value. As you can see my output doesn't match yours so you may need to tweek the CASE statement. I also included the "nextVal1" column so you could see what was being compared.declare @t table (X int, Y int, Val1 int)insert @tselect 1, 1, 20 union allselect 2, 1, 10 union allselect 3, 1, 10 union allselect 4, 1, 20 union allselect 5, 2, 15 union allselect 6, 2, 17 union allselect 7, 2, 15 union allselect 8, 3, 18 union allselect 9, 3, 18 union allselect 10, 3, 18 union allselect 11, 4, 20 union allselect 12, 4, 23 union allselect 13, 4, 10select a.x ,a.y ,a.val1 ,b.val1 [NextVal1] ,case when a.Val1 <> isNull(b.Val1,-1) THEN 0 ELSE a.Val1 end as [val2]from @t aleft outer join @t b on b.y = a.y --within same Y group and b.x-1 = a.x --self join to "next" value as defined by X sequenceOUTPUT:x y val1 NextVal1 val2----------- ----------- ----------- ----------- -----------1 1 20 10 02 1 10 10 103 1 10 20 04 1 20 NULL 05 2 15 17 06 2 17 15 07 2 15 NULL 08 3 18 18 189 3 18 18 1810 3 18 NULL 011 4 20 23 012 4 23 10 013 4 10 NULL 0 Be One with the OptimizerTG |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2008-11-07 : 23:27:26
|
The criteria you mentioned does not match your results. here is what you describedquote: Val2 is 0 if within the same group of Y ,Val1 is not the same asthe next value of Val1 ,otherwise Val1 =Val2'
Declare @Mytable table (x int,y int,val1 int)insert into @Mytable(x,y,val1)select 1, 1, 20 Union Allselect 2, 1, 10 Union Allselect 3, 1, 10 Union Allselect 4, 1, 20 Union Allselect 5, 2, 15 Union Allselect 6, 2, 17 Union Allselect 7, 2, 15 Union Allselect 8, 3, 18 Union Allselect 9, 3, 18 Union Allselect 10, 3, 18 Union Allselect 11, 4, 20 Union Allselect 12, 4, 23 Union Allselect 13, 4, 10 Select a.x ,a.y ,a.Val1 , case when b.y is null then 0 when a.y = b.y and a.Val1 <> b.val1 then 0 else a.val1 end as Val2 from @mytable a Left Join @mytable b on a.x = b.x + 1 Here is the query if you want to 0 it out if Y is not = to the next rowDeclare @Mytable table (x int,y int,val1 int)insert into @Mytable(x,y,val1)select 1, 1, 20 Union Allselect 2, 1, 10 Union Allselect 3, 1, 10 Union Allselect 4, 1, 20 Union Allselect 5, 2, 15 Union Allselect 6, 2, 17 Union Allselect 7, 2, 15 Union Allselect 8, 3, 18 Union Allselect 9, 3, 18 Union Allselect 10, 3, 18 Union Allselect 11, 4, 20 Union Allselect 12, 4, 23 Union Allselect 13, 4, 10 Select a.x ,a.y ,a.Val1 , case when b.y is null then 0 when a.y = b.y and a.Val1 <> b.val1 then 0 when a.y <> b.y then 0 else a.val1 end as Val2 from @mytable a Left Join @mytable b on a.x = b.x + 1 please clarify what you want, the results you posted do not match your request. Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2008-11-07 : 23:28:53
|
I hate when I don't refresh my screen!!! :) Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-08 : 00:50:18
|
Not sure if you really meant thisselect a.x ,a.y ,a.val1 ,case when a.Val1 = isNull(b.Val1,-1) OR a.Val1 = isNull(c.Val1,-1) THEN a.Val1 ELSE 0 end as [val2]from @t aleft outer join @t b on b.y = a.y and b.x-1 = a.xleft outer join @t c on c.y=a.y and c.x+1=a.x this will give you desired sample output. But obviously your description doesnt suit this. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-08 : 00:54:37
|
And just in case the values of x doesnt follow continuosly (ie. you may have gaps due to deletion). then use belowselect a.x ,a.y ,a.val1 ,b.val1 ,c.val1 ,case when a.Val1 = isNull(b.Val1,-1) OR a.Val1 = isNull(c.Val1,-1) THEN a.Val1 ELSE 0 end as [val2]from @t aouter apply(select top 1 Val1 from @t where y = a.y and x > a.x order by x)bouter apply(select top 1 Val1 from @t where y = a.y and x < a.x order by x desc)c |
 |
|
|
ucal
Yak Posting Veteran
72 Posts |
Posted - 2008-11-08 : 08:45:45
|
| Great thanks to TG,Vinnie881 and visakh16.visakh16, your solution got me to where I wanted to be.Now I want to adjust your code to address his situation.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 1 |
 |
|
|
|
|
|