Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I have a following table (id,value)id value1 168907952 168907963 168908224 168908275 168908326 168908337 168908358 16950971I 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 value21 2 16890795 168907963 4 16890822 168908275 6 16890832 168908337 8 16890835 16890835Thanks 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 Value2from [YourTable] Table1 inner join [YourTable] Table2 on Table1.id = Table2.id - 1where Table1.id % 2 = 1