|
MSquared
Yak Posting Veteran
52 Posts |
Posted - 2010-08-17 : 12:57:32
|
You can use a tally table. I've attached the code for creating a tally table on the fly, but it will probably be faster if it already exists.declare @t table (division varchar(400), Div_Num int)insert into @tselect 'Dallas Lab NT10', 101710 union allselect 'Dallas Lab NT10', 101713 union allselect 'Dallas Lab NT10', 101714 union allselect 'Dallas Lab NT10', 101715 union allselect 'Dallas Lab NT10', 101716 union allselect 'Dallas Lab NT10', 101717 union allselect 'Dallas Lab NT10', 101718 union allselect 'Dallas Lab ST10', 2475 union allselect 'Dallas Lab ST10', 2487 union allselect 'Dallas Lab ST10', 2498 union allselect 'Dallas Lab ST10', 2499 union allselect 'Dallas Lab ST10', 2500 Declare @tally table (i int);WITH tenRows AS ( SELECT i FROM ( SELECT 1 AS [1], 2 AS [2], 3 AS [3], 4 AS [4], 5 AS [5], 6 AS [6], 7 AS [7], 8 AS , 9 AS [9], 10 AS [10] ) AS p UNPIVOT (i FOR numbers IN ([1],[2],[3],[4],[5],[6],[7], ,[9],[10]) ) as unpvt ), thousandRows AS ( SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) as n FROM tenRows AS a CROSS JOIN tenRows AS b CROSS JOIN tenRows AS c ) , millionRows AS ( SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS n FROM thousandRows AS a CROSS JOIN thousandRows AS b ) insert into @tally select n from millionRows where n < (select Max(Div_num) from @t)select c.Division, b.i Div_Num from @tally b inner join (select Division, min(Div_Num) MinDivNum, max(Div_Num) MaxDivNum from @t group by Division) a on b.i between a.MinDivNum and a.MaxDivNum left outer join @t c on b.i = c.Div_Num |
 |
|