That’s all, the nice feature about the solution is that its highly expandable; to configure it for greater no. of columns, we just need to modify the pivot/unpivot clauses. Here’s the complete source code:

Also, if you want it strictly in a big single select query, you can replace CTEs by “derived tables”. Here’s the same solution:

--sort vertically and select distinct
select distinct c1,c2,c3
from
(
--split into columns again (based on that sorting)
select rowID, [1] as c1, [2] as c2, [3] as c3
from
(
--sort values in each row (grouped by rowID). This sorts the original data horizontally
select rowID, value, ROW_NUMBER() over (partition by rowID order by value) as colID
from
(
--combine columns into a single row.
select rowID, value
from
(
--add an extra column for identification of a row
select c1, c2, c3, ROW_NUMBER() over (order by @@identity) as rowID
from @t
) as tableWithRowID
unpivot (value for cols in (c1,c2,c3) ) as unpvt
) as combined
) as sorted
pivot ( min(value) for colID in ([1],[2],[3])) as pvt
) as splitted
order by c1, c2, c3