Avatar of pcomb
pcomb
Flag for United States of America asked on

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
1,a,b
2,b,c
3,d,e
4,e,f
Oracle DatabaseMicrosoft SQL Server

Avatar of undefined
Last Comment
PortletPaul

8/22/2022 - Mon
slightwv (䄆 Netminder)

This works for Oracle and should work for SQL Server.

I returned the sequence so it returns:
  SEQUENCE T N
---------- - -
         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

ASKER CERTIFIED SOLUTION
PortletPaul

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
pcomb

ASKER
slightwv thank you this looks good. I got it working for Oracle but Sql Server 2005 does not support lead
PortletPaul

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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
pcomb

ASKER
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
 SEQUENCE T N
---------- - -
         1 a b
         2 b c
         4 d e
         5 e f

thanks
awking00

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.id, t1.sequence, t1.type type1, t2.type type2
 from yourtable t1, yourtable t2
 where t1.id = t2.id
   and t1. sequence  + 1 = t2.sequence) as x  ==> "as x" is not needed for Oracle
;
awking00

Perhaps I should have named the newid alias to newsequence instead, but the query will, in fact, return 1,2,3,4.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
PortletPaul

Nice cross-dbms solution awking00!

I would recommend  ANSI inner join syntax
SELECT
      ROW_NUMBER() OVER (ORDER BY sequence) NEWSEQENCE
    , type1
    , type2
FROM (
            SELECT
                  T1.id
                , T1.sequence
                , T1.type TYPE1
                , T2.type TYPE2
            FROM yourtable T1
                  INNER JOIN yourtable T2 ON T1.id = T2.id 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