liniking adjacent rows in MS Sql Server 2005 and Oracle 10g

I have a set of data and I want to create pairs from each adjacent row. Pairs are only relevant if they have the same id eg

ID, sequence, type
1, 1, a
1, 2, b
1, 3, c
2, 4, d
2, 5, e
2, 6, f

create a set of rows as follows
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
This works for Oracle and should work for SQL Server.

I returned the sequence so it returns:
---------- - -
         1 a b
         2 b c
         4 d e
         5 e f

Open in new window

Not 1,2,3,4.  In Oracle replacing the outer sequence with rownum will return 1,2,3,4.  Not sure what the SQL Server equivalent is.

select sequence, type, next_type from (
select sequence, type, lead(type) over(partition by id order by sequence) next_type
from tab1
where next_type is not null

Open in new window

PortletPaulEE Topic AdvisorCommented:
>>"Pairs are only relevant if they have the same id"

I cannot follow how that statement works with the examples

there are 3 rows with an ID of 1, and 3 rows with an id of 2 making only 2 result rows

If the quote above is true; how on earth do you get any more than 2 rows as a result

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pcombAuthor Commented:
slightwv thank you this looks good. I got it working for Oracle but Sql Server 2005 does not support lead
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

PortletPaulEE Topic AdvisorCommented:
Again i don't  follow. The results by slightwv don't  match those provided  in thee question.

Could you check the sample data and results please  or verify that  the results  by slightwv as re the correct  ones for the sample data .

Lead  isn't  available  until   mssql 2012 i believe. Its more complex but can be done - if i knew what  the results should be.
pcombAuthor Commented:
PortletPaul slightwvs results do match except the sequence has gaps which is not critical it just needs to  be unique. The key part is to ahve the pairs eg a,b or b.c as rows

If you know a way to do this in sqlserver 2005 that would be great
---------- - -
         1 a b
         2 b c
         4 d e
         5 e f

awking00Information Technology SpecialistCommented:
This should work on both Oracle and SQL Server, with the caveat that SQL Server requires a subquery alias -
select row_number() over (order by sequence) newid, type1, type2 from
(select, t1.sequence, t1.type type1, t2.type type2
 from yourtable t1, yourtable t2
 where =
   and t1. sequence  + 1 = t2.sequence) as x  ==> "as x" is not needed for Oracle
awking00Information Technology SpecialistCommented:
Perhaps I should have named the newid alias to newsequence instead, but the query will, in fact, return 1,2,3,4.
PortletPaulEE Topic AdvisorCommented:
Nice cross-dbms solution awking00!

I would recommend  ANSI inner join syntax
    , type1
    , type2
                , T1.sequence
                , T1.type TYPE1
                , T2.type TYPE2
            FROM yourtable T1
                  INNER JOIN yourtable T2 ON = AND T1.sequence + 1 = T2.sequence
      ) X

Open in new window

(with that alias X just leave out "AS " and then it will work for both platforms)

no points please
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.