Click here to Skip to main content
15,909,953 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a table like this:

col1|col2
----| ---
a....|.e
b....|.f
c....|.g
d....|.h


i want to check that item in col2 next row item in col1
ie, item 'e' in col2 eqals to item 'b' in col1.
Is it possible in sql server???
Posted
Updated 15-Jun-12 2:01am
v2

1 solution

I have a solution but it is only possible if you have a column for sequence number too:

let me tell you what i did

the table:
a --------- b --------- seq
-----------------------------
a --------- b --------- 1
b --------- b --------- 2
c --------- b --------- 3
d --------- e --------- 4
e --------- f --------- 5
f --------- b --------- 6
g --------- b --------- 7
h --------- i --------- 8
i --------- a --------- 9


column name shown in bold. data in normal font and underlined values are possible candidate to come out in correct answer.

Now for this table the following query will give you the desired results;

SQL
with tempTable as
(
select t1.a as val, ROW_NUMBER() over (order by t1.seq) as newRowId from table_1 t1, table_1 t2 where (t1.seq - t2.seq) = 1
)

select distinct t3.b from Table_1 t3, tempTable T where T.newRowId = t3.seq and t3.b = val


the result will be

b
e
f
i



I hope you understood what I did and hopefully the query will help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900