Question

my table looks like:

column 1 | column 2
--------------------
   A         L
   B         M
   C         N
   D         O
   E         P
   F         Q
   G         R
   H         S
   I         T
   J         U
   K         V

Now I want to "concatenate" fractions of n-rows (n is variable) together.

For example:

-) if n = 4, the output should be:

column 1 | column 2 | column 1 | column 2 | column 1 | column 2
   A          L          E          P          I          T
   B          M          F          Q          J          U
   C          N          G          R          K          V
   D          O          H          S

-) if n = 10, the output should be:

column 1 | column 2 | column 1 | column 2
   A          L          K          V
   B          M
   C          N
   D          O
   E          P
   F          Q
   G          R
   H          S
   I          T
   J          U

I thought about using a temp table and a loop. After every iteration, I select fractions of n-rows and concatenate them together (with union). However, I think this solution could be a little slow for large datasets.

Maybe someone of you knows a better approach?

I would really appreciate if someone could help me with this.

Kind Regards

Bernhard

No correct solution

OTHER TIPS

this is not complete

pivot is left,i was thinking of doing something like this,

Declare @n int=4
Declare @j int
Declare @k int
Select  @j=count(*) from #t
if(@j%@n=0)
set @k= (@j/@n)
else
set @k= (@j/@n)+1
--select @k
;with CTE as
( 
select *,ntile(@k)over(order by column1)rn from #t
)
select * from cte
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top