sql - TSQL Convert rows to columns -
i have query returns data looks this:
description ---------- thing1 thing2 thing3 thing4 thing5 thing6 thing7
i make data this:
desc1 desc2 desc3 ----- ----- ----- thing1 thing2 thing3 thing4 thing5 thing6 thing7
could provide example on how this?
thanks!
you stated comments order of data in desc
columns not matter.
if case, can use following implement ntile
, row_number()
:
;with cte ( select description, 'desc'+cast(ntile(3) over(order description) varchar(10)) col yt ) select desc1, desc2, desc3 ( select description, col, row_number() over(partition col order col) rn cte ) d pivot ( max(description) col in (desc1, desc2, desc3) ) piv;
see sql fiddle demo.
the ntile
function distributes rows separate groups. once done, apply row_number()
give unique number each row while grouping.
this gives result:
| desc1 | desc2 | desc3 | ---------------------------- | thing1 | thing4 | thing6 | | thing2 | thing5 | thing7 | | thing3 | (null) | (null) |
Comments
Post a Comment