I have a noobie question:
I need to write SQL script that returns 3 columns results set from a given one column table @T with integer field N, e.g.:
declare @t table(n int)
insert into @t(n) values (3),(4),(5),
(6),(7),(8),
(9),(10),(11),
(13),(14),(15),
(16),(17),(18), (19)
The result 3 columns table should contains data from the table @T row by row in ascending order. If there is a missed values (e.g. {2,4} 3 is missing here) in the table @T, then the corresponding cell in result set should be NULL.
Example 1:
For table with values {2,3,4,5} the result table should looks like:
NULL 2 3
4 5 NULL
Example 2:
For table with values {2,4} the result table should looks like:
NULL 2 NULL
4 NULL NULL
You can use a numbers table like this:
Working sample using
master..spt_valuesas a numbers table.Or you can use a numbers table with pivot: