Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help with Pairing of Table Rows

Author  Topic 

sasisekhara
Starting Member

1 Post

Posted - 2007-04-27 : 17:45:11
I have a following table (id,value)
id value
1 16890795
2 16890796
3 16890822
4 16890827
5 16890832
6 16890833
7 16890835
8 16950971

I want to pair each 2 table rows (e.g.) and output new table with values and id's in one string for each pair (with each id is used only once). Any idea how to do this?

e.g.

id1 id2 value1 value2
1 2 16890795 16890796
3 4 16890822 16890827
5 6 16890832 16890833
7 8 16890835 16890835


Thanks a lot,
Sasi

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2007-04-27 : 18:57:12
This assumes that you id values are sequential and continuous. Things get more complicated if this is not the case.
select	Table1.id as id1,
Table2.id as id2,
Table1.value as Value1,
Table2.value as Value2
from [YourTable] Table1
inner join [YourTable] Table2 on Table1.id = Table2.id - 1
where Table1.id % 2 = 1


e4 d5 xd5 Nf6
Go to Top of Page
   

- Advertisement -