You stated the comments that the order of the data in the Desc
columns does not matter.
If that is the case, then you can use the following which implement NTILE
and row_number()
:
;with cte as
(
select description,
'desc'+cast(ntile(3) over(order by Description) as varchar(10)) col
from yt
)
select desc1, desc2, desc3
from
(
select description, col,
row_number() over(partition by col order by col) rn
from cte
) d
pivot
(
max(description)
for col in (desc1, desc2, desc3)
) piv;
See SQL Fiddle with Demo.
The NTILE
function distributes the rows into separate groups. Once that is done, then apply a row_number()
to give a unique number to each row while grouping.
This gives a result:
| DESC1 | DESC2 | DESC3 |
----------------------------
| Thing1 | Thing4 | Thing6 |
| Thing2 | Thing5 | Thing7 |
| Thing3 | (null) | (null) |
id
field, or just order byDescription
(which won't work once you hit 10)?Thing3
goes inDesc3
? Why notDesc1
?