sql - Take two rows of data at once and put the data into two columns? -
what sql code transform input table output table in example? (assume ordering, odd rows go column2 , rows go column3 consecutive odd-even pairs on same output row.)
input table: column1 b c d e f output table: column2 column3 b c d e f
update: assuming ordering, odd rows go column2 , rows go column3 consecutive odd-even pairs on same output row
try this:
;with cte ( select row_number() on (order column1) rn, column1 #mytable ), cte2 ( select * cte rn % 2 <> 0 ), cte3 ( select * cte rn % 2 = 0 ) select c2.column1 column2, c3.column1 column3 cte2 c2 left join cte3 c3 on c2.rn = c3.rn - 1
cte2
contains odd rows, whereas cte3
contains rows. join consecutive odd - pairs c2.rn = c3.rn - 1
desired result set.
Comments
Post a Comment